C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Omitting unspecialized template function definitions.

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Al
Guest





PostPosted: Sat May 12, 2007 9:10 am    Post subject: Omitting unspecialized template function definitions. Reply with quote



Hi,

Is it legal to declare a template function with no unspecialized
definition, only definitions for the used specializations?

template <class T>
T foo();

template <> int foo() { ...; }
template <> float foo() { ...; }

It seems to work on a couple of platforms. This way there's no need to
throw "not implemented" at runtime, since using a wrong version would
always be caught at link time (presumably).

Thanks.
-Al-

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Rune Allnor
Guest





PostPosted: Sat May 12, 2007 7:04 pm    Post subject: Re: Omitting unspecialized template function definitions. Reply with quote



On 12 May, 11:11, Al <t...@haik.us> wrote:
Quote:
Hi,

Is it legal to declare a template function with no unspecialized
definition, only definitions for the used specializations?

template <class T
T foo();

template <> int foo() { ...; }
template <> float foo() { ...; }

It seems to work on a couple of platforms. This way there's no need to
throw "not implemented" at runtime, since using a wrong version would
always be caught at link time (presumably).

Wouldn't what you ask for be taken care of by virtual functions? If this
can be done your way, you would have to declare at coding time exactly
what types to implement somewhere -- which is a requirement of virtual
functions as well. The basic philosophy behind template that "the type
of the argument is unknown at coding time" is broken, since you at
coding time have this finite and well-specified list of types you allow.

I can't really see that templates are the tool for the job? Or am I
missing some very subtle detail in either the problem specification
or the proposed solution?

Rune


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Sebastian Redl
Guest





PostPosted: Sat May 12, 2007 7:54 pm    Post subject: Re: Omitting unspecialized template function definitions. Reply with quote



On Sat, 12 May 2007, Al wrote:

Quote:
Is it legal to declare a template function with no unspecialized
definition, only definitions for the used specializations?

Yes, absolutely legal and always caught at link time. (Unless, of course,
you have a output type that isn't linked completely, such as static
libraries or, I believe, POSIX shared objects.)

Sebastian Redl

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Al
Guest





PostPosted: Sat May 12, 2007 7:54 pm    Post subject: Re: Omitting unspecialized template function definitions. Reply with quote

Hi,

Rune Allnor wrote:
<snip>
Quote:
Wouldn't what you ask for be taken care of by virtual functions? If this
can be done your way, you would have to declare at coding time exactly
what types to implement somewhere -- which is a requirement of virtual
functions as well. The basic philosophy behind template that "the type
of the argument is unknown at coding time" is broken, since you at
coding time have this finite and well-specified list of types you allow.

I don't think that "basic philosophy" is comprehensive of all template
uses. I think of templates as a general-purpose meta-programming tool.

Quote:
I can't really see that templates are the tool for the job? Or am I
missing some very subtle detail in either the problem specification
or the proposed solution?

I don't think the task at hand is particularly subtle. I am using a
boost::variant (a true discriminated union) with a finite set of types
(e.g. float, int, string and a few more).

I don't think invoking the full OO machinery is necessary for
dispatching these types when a simple template (with the help of
boost::static_visitor) will work. I just wanted to know if the technique
is a legal one.

Thanks,
-Al-


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Daniel Krügler
Guest





PostPosted: Sun May 13, 2007 2:39 am    Post subject: Re: Omitting unspecialized template function definitions. Reply with quote

On 12 Mai, 21:54, Sebastian Redl <e0226...@stud3.tuwien.ac.at> wrote:
Quote:
On Sat, 12 May 2007, Al wrote:
Is it legal to declare a template function with no unspecialized
definition, only definitions for the used specializations?

Yes, absolutely legal and always caught at link time. (Unless, of course,
you have a output type that isn't linked completely, such as static
libraries or, I believe, POSIX shared objects.)

Nope, from the standpoint of the language this is illegal
and causes UB, because it violates the ODR.
The concept of "caught at link time" does not exist in
C++, although you are right that this is a typical
implementation behaviour. I hope that my corresponding
answer will come up soon, where I provide more
details.

Btw.: Thanks for correcting my error to qualify a
class invariant as a postcondition in another posting.

Greetings from Bremen,

Daniel Krügler



--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Alberto Ganesh Barbati
Guest





PostPosted: Tue May 15, 2007 12:38 am    Post subject: Re: Omitting unspecialized template function definitions. Reply with quote

Richard Corden ha scritto:
Quote:
Al wrote:

template <class T
T foo();

template <> int foo() { ...; }
template <> float foo() { ...; }

[As a BTW - this exact example is 'strictly' ill-formed, since 'T' can
only be deduced from the function argument types - however AFAIK most,
if not all, compilers do the right thing(tm). ]

Why do you say it's ill-formed? Could you please elaborate? I think it
is perfectly legal. Of course, the only way to invoke such a template is
to explicitly specify the template parameters, as in foo<int>(), but
this fact doesn't make the *definitions* ill-formed.

Ganesh

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Abhishek Padmanabh
Guest





PostPosted: Wed May 16, 2007 3:41 pm    Post subject: Re: Omitting unspecialized template function definitions. Reply with quote

On 14 May, 18:58, Richard Corden <richard_cor...@hotmail.com> wrote:
Quote:
// a2.cc
template <class T> T foo ();
template <> int foo<int>() {}
void b2 () { foo<int> (); }

The above is either 'ill-formed no diagnostic required', or just
undefined behaviour.


What's the problem with b2 function in the part a2.cc?


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Richard Corden
Guest





PostPosted: Wed May 16, 2007 5:19 pm    Post subject: Re: Omitting unspecialized template function definitions. Reply with quote

Abhishek Padmanabh wrote:
Quote:
On 14 May, 18:58, Richard Corden <richard_cor...@hotmail.com> wrote:
// a2.cc
template <class T> T foo ();
template <> int foo<int>() {}
void b2 () { foo<int> (); }

The above is either 'ill-formed no diagnostic required', or just
undefined behaviour.


What's the problem with b2 function in the part a2.cc?

I'm not an expert on what actually happens regarding implicit
instantiation matching by linkers etc, however, I think the problem is
that a1.o and a2.o will both have a different definitions for
'foo<int>'. This then leads to:

1) linker error, or
2) definition from a1.cc being used, or
3) definition from a2.cc being used, or
4) other.

The standard just says that the explicit specialization shall be
declared before the first use of that specialization would cause an
implicit instantiation to take place, no diagnostic required.[14.7.3/6]


Regards,

Richard

--
Richard Corden

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.