Archived

This topic is now archived and is closed to further replies.

Template strangeness

This topic is 5130 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m really curious about something... When you declare templates, you can do things like: template int test() { return N; } And it compiles just fine; but when you replace the int with a float or double it doesn''t compile O_o. Why is this?

Share this post


Link to post
Share on other sites
quote:
Original post by Puzzler183
I'm really curious about something... When you declare templates, you can do things like:

template int test()
{
return N;
}

And it compiles just fine; but when you replace the int with a float or double it doesn't compile O_o. Why is this?


Because a non-type template parameter must be of one of the following

— an integral constant-expression of integral or enumeration type; or
— the name of a non-type template-parameter; or
— the name of an object or function with external linkage, including function templates and function
template-ids but excluding non-static class members, expressed as id-expression; or
— the address of an object or function with external linkage, including function templates and function
template-ids but excluding non-static class members, expressed as & id-expression where the & is
optional if the name refers to a function or array; or
— a pointer to member

As specified in 14.3.2 of the C++ standard

[edited by - Jingo on November 29, 2003 8:40:30 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Jingo

Because a non-type template parameter must be of one of the following

— an integral constant-expression of integral or enumeration type; or
— the name of a non-type template-parameter; or
— the name of an object or function with external linkage, including function templates and function
template-ids but excluding non-static class members, expressed as id-expression; or
— the address of an object or function with external linkage, including function templates and function
template-ids but excluding non-static class members, expressed as & id-expression where the & is
optional if the name refers to a function or array; or
— a pointer to member

As specified in 14.3.2 of the C++ standard

[edited by - Jingo on November 29, 2003 8:40:30 PM]


But you didn''t answer my question... I know the standard says so, but why? What is the point of restricting things like that?

Share this post


Link to post
Share on other sites