Template ctor - explicitly specify templ param

A

Agoston Bejo

Platform: VC++ 7.1

Hello,
suppose I've got a template constructor in a class, and I would like to

explicitly specify one of its parameters. What is the syntax for that? Or is

there no such syntax? It would mean that template constructor functions
would

have "narrower semantics" than ordinary member functions (by which I mean
that

they cannot be used in every way ordinary functions can be used), which
somehow

feels "wrong" to me.
An example:


------------------------------

#include <iostream>

using namespace std;

struct A {

template<int N, typename T>
A(T t1, T t2) {
i = (int)t1 + (int)t2 + N;
}

int i;
};


int _tmain(int argc, _TCHAR* argv[])
{
A a<5>(7.5, 8.5); // ERROR
cout << a.i << endl;
return 0;
}

--------------
Error messages: (both for the same line)

error C2143: syntax error : missing ';' before '<'
error C2512: 'A' : no appropriate default constructor available


Thx,
Agoston
 
J

Jonathan Turkanis

Agoston said:
Platform: VC++ 7.1

Hello,
suppose I've got a template constructor in a class, and I would like
to explicitly specify one of its parameters. What is the syntax for
that? Or is there no such syntax?

Right, there is no such syntax.

Occasionally when I would otherwise like to specify a constuctor template
parameter explicitly I use a dummy argument which allows the parameter to be
deduced. E.g., instead of

struct Class {
template<typename T>
Class() { ... }
};

write

struct Class {
template<typename T>
Class(boost::mpl::identity<T>) { ... }
};

This is a poor solution if instances of Class must be constructed in user-level
code, but it's fine for behind the scenes.

Jonathan
 

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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top