Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Hoe to specialize member constructor templates with non-types

35 views
Skip to first unread message

Philip

unread,
Jan 27, 2012, 6:18:08 PM1/27/12
to
Using VS 2005 the following compiles but uncomment one, other, or both
pieces of code and I get the listed compiler errors. VS 2005 bug or
limitation or something I am missing?

Philip

enum MyEnum { myEnum };
class Test
{
public:
Test();
template<MyEnum, typename t>
Test(t);
template<MyEnum, typename t>
void memFunc(t);
};
// fails with
// error C2143: syntax error : missing ';' before '<'
// error C2350: 'Test::{ctor}' is not a static member
// template<>
// Test::Test<myEnum, int>(int)
// {
// }
template<MyEnum, typename t>
Test::Test(t)
{
}
// works
template<>
void Test::memFunc<myEnum, int>(int)
{
}
template<MyEnum, typename t>
void Test::memFunc(t)
{
}

void test1()
{
// fails with
// error C2143: syntax error : missing ';' before '<'
// Test test1<myEnum>(1);

// works
Test test2;
test2.memFunc<myEnum>(1);
}


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

Daniel Krügler

unread,
Jan 28, 2012, 1:12:24 PM1/28/12
to
Am 28.01.2012 00:18, schrieb Philip:
> Using VS 2005 the following compiles but uncomment one, other, or both
> pieces of code and I get the listed compiler errors. VS 2005 bug or
> limitation or something I am missing?

Both ;-) I see no problem in your out-of-class definition of the
constructor specialization, so this looks like a compiler defect to me.

But there is no way to provide explicit template parameters for a
constructor, the line

Test test1<myEnum>(1);

does not realize this and the code is ill-formed. Providing template
parameters for constructors are useless in C++03 (and it impossible to
instantiate the constructor). In C++11 there can be used when also a
default template argument is provided, e.g. to sfinae-constrain such a
constructor.

HTH & Greetings from Bremen,

Daniel Krügler

Philip

unread,
Jan 28, 2012, 3:30:15 PM1/28/12
to
> But there is no way to provide explicit template parameters for a
> constructor, the line
>
> Test test1<myEnum>(1);
>
> does not realize this and the code is ill-formed. Providing template
> parameters for constructors are useless in C++03 (and it impossible to
> instantiate the constructor). In C++11 there can be used when also a
> default template argument is provided, e.g. to sfinae-constrain such a
> constructor.

Many thanks. I think I will have to build a factory static member to
accomplish what I need.

It seems odd to me that template parameters can be provide for a
templated member function but not for a templated constructor.

I wonder why this is not aprtof the specification.

Philip

Daniel Krügler

unread,
Jan 29, 2012, 5:43:30 PM1/29/12
to
Am 28.01.2012 21:30, schrieb Philip:
[..]
> It seems odd to me that template parameters can be provide for a
> templated member function but not for a templated constructor.
>
> I wonder why this is not aprtof the specification.

One of the problems to solve would be to specify the correct syntax to call the constructor specializations. Note that e.g. providing explicit template arguments for overloaded operators or conversion functions also require to invoke the operator by "name", as in the following example:

struct A {
template<class T>
T operator[](int);
} a;

int main() {
a.operator[]<bool>(0);
}

While there exists a syntactic way to provide here the template parameters, the actual advantage of the simplifying operator syntax got lost. For constructors the situation is worse. In C++03 constructors could never be named, in C++11 there is only a single situation where this is possible, namely when declaring an inherited constructor set via a using-declaration, as in:

struct B {
B(int);
B(double, bool);
};

struct D : B {
using B::B;
};

To add a way to specify explicit template template arguments for a constructor template would require a new syntactic form, because there is no analogous function-like way to call a constructor. I have the feeling that there is no big pressure to realize that, because I expect that it syntactic complexity would be similarly "unnatural" as for conversion function template or operator function templates.

HTH & Greetings from Bremen,

Daniel Krügler



0 new messages