How to call templated constructor explicitly?

L

Lawrence Spector

How does one call a templated constructor inside of a class when
instantiating an object? I made up a quick sample to demonstrate.

#include <iostream>

class TestClass
{
public:
template <class T>
TestClass()
{
std::cout << "Called with type " << typeid(T).name() << std::endl;
}
};

int main()
{
TestClass testClass; // Error: How do I call the constructor and tell
it that I want to use an int for the template type?

std::cin.get();

return 0;
}

If it was done implicitly, where T was used in the parameter list of
the constructor, then it would be easy, but I actually have a case
where I need to be explicit.

Thanks in advance,

Lawrence
 
V

Victor Bazarov

Lawrence said:
How does one call a templated constructor inside of a class when
instantiating an object? I made up a quick sample to demonstrate.

#include <iostream>

class TestClass
{
public:
template <class T>
TestClass()
{
std::cout << "Called with type " << typeid(T).name() << std::endl;
}
};

int main()
{
TestClass testClass; // Error: How do I call the constructor and tell
it that I want to use an int for the template type?

std::cin.get();

return 0;
}

If it was done implicitly, where T was used in the parameter list of
the constructor, then it would be easy, but I actually have a case
where I need to be explicit.

There is no way to do that. Any templated constructor has to have the
argument from which the compiler will deduce the template arguments.
In your case you need to supply (at least a bogus) argument, like

template<class T> TestClass(T* bogus) {
...
}

which you will supply as

TestClass testClass((int*)0); // means 'T' is 'int'.

You could invent some other (nicer, perhaps) way of providing the bogus
argument.

V
 
L

Lawrence Spector

Lawrence said:
How does one call a templated constructor inside of a class when
instantiating an object? I made up a quick sample to demonstrate.
#include <iostream>
class TestClass
{
public:
template <class T>
TestClass()
{
std::cout << "Called with type " << typeid(T).name() << std::endl;
}
};
int main()
{
TestClass testClass; // Error: How do I call the constructor and tell
it that I want to use an int for the template type?

return 0;
}
If it was done implicitly, where T was used in the parameter list of
the constructor, then it would be easy, but I actually have a case
where I need to be explicit.

There is no way to do that. Any templated constructor has to have the
argument from which the compiler will deduce the template arguments.
In your case you need to supply (at least a bogus) argument, like

template<class T> TestClass(T* bogus) {
...
}

which you will supply as

TestClass testClass((int*)0); // means 'T' is 'int'.

You could invent some other (nicer, perhaps) way of providing the bogus
argument.

V


I see. Thanks for the prompt reply. I guess to make it look a little
nicer, I could use a factory when I create it.

Thanks for the assistance,

Lawrence
 
K

Kai-Uwe Bux

Lawrence said:
How does one call a templated constructor inside of a class when
instantiating an object? I made up a quick sample to demonstrate.

#include <iostream>

class TestClass
{
public:
template <class T>
TestClass()
{
std::cout << "Called with type " << typeid(T).name() << std::endl;
}
};

int main()
{
TestClass testClass; // Error: How do I call the constructor and tell
it that I want to use an int for the template type?

std::cin.get();

return 0;
}

I think, that has no chance of working.
If it was done implicitly, where T was used in the parameter list of
the constructor, then it would be easy, but I actually have a case
where I need to be explicit.

As a work-around, you could introduce a dummy parameter:

template <class T >
TestClass ( T* )
{
std::cout << "Called with type " << typeid(T).name() << std::endl;
}

and construct the object via:

TestClass test_object ( (some_type*)0 );


Best

Kai-Uwe Bux
 

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
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top