Default for template parameter - how to make it work?

Y

Yan

I have the following code that the compiler complains about that "No
parameters provided for template". The compiler is the one that comes
with Sun Solaris, not sure what exactly. I have no control over what
compiler is to be used, so I just want to know whether the code is
wrong (in which case could you please show me the correct version) or
the compiler isn't up-to-date.

Here is the code:
---------------------------------
template <typename T = int> class C {};

int main() {
C c; // that is the line that the compiler is complaining about
return 0;
}
 
Y

Yan

I have the following code that the compiler complains about that "No
parameters provided for template". The compiler is the one that comes
with Sun Solaris, not sure what exactly. I have no control over what
compiler is to be used, so I just want to know whether the code is
wrong (in which case could you please show me the correct version) or
the compiler isn't up-to-date.

Here is the code:
---------------------------------
template <typename T = int> class C {};

int main() {
C c; // that is the line that the compiler is complaining about
return 0;}

Changing the line in question to "C<int> c" makes the compilation
error go away, but defeats the purpose. Anyways, if someone could
please tell me whether there is a problem with the code I posted or
with the compiler I am using I'd greatly appreciate that.
 
V

Victor Bazarov

Yan said:
Changing the line in question to "C<int> c" makes the compilation
error go away, but defeats the purpose. Anyways, if someone could
please tell me whether there is a problem with the code I posted or
with the compiler I am using I'd greatly appreciate that.

Try

C<> c;

The point here is that 'C' is a "template-id" and it has to be followed
by the angle bracket _outside_ the template itself.

V
 
Y

Yan

Try

C<> c;

The point here is that 'C' is a "template-id" and it has to be followed
by the angle bracket _outside_ the template itself.

V

Thanks Victor,

Unfortunately it also defeats the purpose in a way. There is this
class that is used all over the place and I want to turn it into a
templated one. But I don't want to make numerous changes wherever it's
used in the code and insert the template parameters. I would much
rather use the default parameters to avoid changes in the 'legacy'
code, but it looks like that is not an option.

Yan
 
V

Victor Bazarov

Yan said:
Thanks Victor,

Unfortunately it also defeats the purpose in a way. There is this
class that is used all over the place and I want to turn it into a
templated one. But I don't want to make numerous changes wherever it's
used in the code and insert the template parameters. I would much
rather use the default parameters to avoid changes in the 'legacy'
code, but it looks like that is not an option.

Then your only option is

template<class T> class C_t { ..
typedef C_t<int> C;

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top