471,319 Members | 1,580 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

How to call templated constructor explicitly?

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

Jul 16 '07 #1
3 11102
Lawrence Spector wrote:
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[s] from which the compiler will deduce the template arguments.
In your case you need to supply (at least a bogus) argument, like

template<class TTestClass(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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 16 '07 #2
On Jul 16, 3:01 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Lawrence Spector wrote:
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[s] from which the compiler will deduce the template arguments.
In your case you need to supply (at least a bogus) argument, like

template<class TTestClass(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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
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

Jul 16 '07 #3
Lawrence Spector wrote:
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
Jul 16 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

23 posts views Thread by Fabian Müller | last post: by
8 posts views Thread by trying_to_learn | last post: by
2 posts views Thread by ferdinand.stefanus | last post: by
12 posts views Thread by Rahul | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.