Skip to main content
GameDev.net gamedev.net
🔒 Locked

Template Constructor Overriding

Started by Geometrian Nov 21, 2011 at 4:20 PM 3 replies 1.1k views
Original Post
Geometrian
Geometrian
Hi,

I have two template constructors, sorta like:[source lang="cpp]template my_obj(T arg0, /* more args */) { /* */ }
template my_obj(const T arg0[], /* more args */) { /* */ }[/source]The problem is, when I construct the object with an array of data, it chooses the first template constructor with T=float*, instead of choosing the second as I would expect. How do I make the compiler do what I want here?

Thanks,
-G
[size="1"]And a Unix user said rm -rf *.* and all was null and void...|There's no place like 127.0.0.1|The Application "Programmer" has unexpectedly quit. An error of type A.M. has occurred.
[size="2"]
RobTheBloke
RobTheBloke
try this:


template<typename T> my_obj(const T& arg0, /* more args */) { /* */ }
template<typename T> my_obj(const T* arg0, /* more args */) { /* */ }
Geometrian
Geometrian
template<typename T> my_obj(const T& arg0, /* more args */) { /* */ }
template<typename T> my_obj(const T* arg0, /* more args */) { /* */ }
[/quote]Yup--that works, thanks! So this is giving the compiler the hint that T should be a singular value, ya?
[size="1"]And a Unix user said rm -rf *.* and all was null and void...|There's no place like 127.0.0.1|The Application "Programmer" has unexpectedly quit. An error of type A.M. has occurred.
[size="2"]
Geometrian
Geometrian
Hmmmmm . . . the fix works for MSVC, but the problem occurs on GCC . . .
Ideas? Thanks,
[size="1"]And a Unix user said rm -rf *.* and all was null and void...|There's no place like 127.0.0.1|The Application "Programmer" has unexpectedly quit. An error of type A.M. has occurred.
[size="2"]
johnstanp
johnstanp

Hi,

I have two template constructors, sorta like:[source lang="cpp]template my_obj(T arg0, /* more args */) { /* */ }
template my_obj(const T arg0[], /* more args */) { /* */ }[/source]The problem is, when I construct the object with an array of data, it chooses the first template constructor with T=float*, instead of choosing the second as I would expect. How do I make the compiler do what I want here?

Thanks,
-G



Replacing T by float*, we have:
my_obj my_obj(const float * arg0, /* more args */ )
my_obj my_obj(const float* arg0[], */ more args */)

If the argument is a float* and T=float*, then the first constructor is called. To have the second constructor be called when T=float*, you need to pass an array of pointers to constant floats. And you don't do that in your example. You need to specialize the template class for a pointer to a type in order to obtain what you expect. I mean you should do this:
[source]
template
class my_obj
{
public:
my_obj(T const * arg0, /* more args */)
};
[/source]

Actually to have the second constructor called when you pass an array of floats (which decays to a pointer to a float), you need to do this, if you want to keep your implementation as posted:
[source]
float vals[16] = {};
my_obj obj(vals, /* more args */);
[/source]

The type you must use is T=float and not T=float*.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.