Template argument deduction question

D

Dilip

This is just an academic question but is there any particular reason
why this does not work?

template<typename T>
class Foo
{
public:
Foo(T x) : myvar_(x) { }
private:
T myvar_;
};

int main()
{
Foo xx(2); // why cannot the compiler deduce T as int?
return 0;
}
 
V

Victor Bazarov

Dilip said:
This is just an academic question but is there any particular reason
why this does not work?

template<typename T>
class Foo
{
public:
Foo(T x) : myvar_(x) { }
private:
T myvar_;
};

int main()
{
Foo xx(2); // why cannot the compiler deduce T as int?
return 0;
}

Well, if I define a specialisation of your Foo template like this:

template<> class Foo<void*>
{
public:
Foo(int blah) {}
};

how should the compiler decide which Foo to use, Foo<int> or my
Foo<void*>? So, the limitation is imposed that *you* have to tell
the compiler what Foo to instantiate:

Foo<int> whatever...

V
 
A

amparikh

Dilip said:
This is just an academic question but is there any particular reason
why this does not work?

template<typename T>
class Foo
{
public:
Foo(T x) : myvar_(x) { }
private:
T myvar_;
};

int main()
{
Foo xx(2); // why cannot the compiler deduce T as int?
return 0;
}

from what i understand, template argument deduction doesnt apply to
class templates.
It only kicks in for functiion templates.

I think I recall reading this from Vandevoorde's book, but anyone else
who knows for sure can confirm this.
 
D

Dilip

from what i understand, template argument deduction doesnt apply to
class templates.
It only kicks in for functiion templates.

I think I recall reading this from Vandevoorde's book, but anyone else
who knows for sure can confirm this.

I know -- that's what I am reading too and that's where I picked up
that example but I wanted to understand *why* that restriction was
imposed. Victor, as usual, came to the resue.

thanks!
 
A

amparikh

Dilip said:
I know -- that's what I am reading too and that's where I picked up
that example but I wanted to understand *why* that restriction was
imposed. Victor, as usual, came to the resue.

thanks!

Unless I mis-understood Victor's messsage, but whatever he has said
should apply to function templates too. You can do full specialization
for function templates and still use parameter deduction where function
overloading and other things will come into picture..
But that's not the case for class templates.
 
V

Victor Bazarov

Unless I mis-understood Victor's messsage, but whatever he has said
should apply to function templates too. You can do full specialization
for function templates and still use parameter deduction where
function overloading and other things will come into picture..
But that's not the case for class templates.

With the functions they are simply added to the list for overload
resolution. This mechanism already exists and is easy to use. With
types (used for object creation) there is no such mechanism. The
name has to represent a concrete (and complete) type. Why? There
has to be an inherent difficulty for compiling tempalate, causing
it to instantiate, figuring out its size, which constructor to call,
etc. It must be much simpler with functions, that's why it exists
there and not with types.

V
 
S

Salt_Peter

Dilip said:
This is just an academic question but is there any particular reason
why this does not work?

template<typename T>
class Foo
{
public:
Foo(T x) : myvar_(x) { }
private:
T myvar_;
};

int main()
{
Foo xx(2); // why cannot the compiler deduce T as int?
return 0;
}

Because the "int" belongs to Foo, not T - which is just a placeholder.
The compiler needs to generate a new version of Foo< > for every type T
needed. The compiler is not allowed to guess which one of those
versions needs to be used either. What you can do, unlike functions, is
set defaults.

#include <iostream>

template< typename T = int >
class Foo
{
T t;
public:
Foo(const T& x = T()) : t(x) { }
const T& get() const { return t; }
};

int main()
{
Foo< > foo(99);
std::cout << "foo = " << foo.get() << std::endl;
return 0;
}
 
A

Andrey Tarasevich

Dilip said:
This is just an academic question but is there any particular reason
why this does not work?

template<typename T>
class Foo
{
public:
Foo(T x) : myvar_(x) { }
private:
T myvar_;
};

int main()
{
Foo xx(2); // why cannot the compiler deduce T as int?
return 0;
}

Because here you really have not one, but two fairly independent templates: a
class template and a member function (constructor) template. '2' is an argument
for the latter, while 'T' is a parameter of the former.

For some purposes you can "link" the two by introducing a "factory" function

template<typename T> Foo<T> make_foo(T x) {
return Foo<T>(x);
}

...
make_foo(2); // returns a temporary of type 'Foo<int>' initialized with '2'

(a technique used, for example, in STL) but, of course, it is only usable for
generating temporary objects and doesn't help with declarations.
 

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,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top