By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,605 Members | 1,370 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,605 IT Pros & Developers. It's quick & easy.

Template argument deduction question

P: n/a

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;
}

Nov 15 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Dilip wrote:
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<intor my
Foo<void*>? So, the limitation is imposed that *you* have to tell
the compiler what Foo to instantiate:

Foo<intwhatever...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 15 '06 #2

P: n/a

Dilip wrote:
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.

Nov 15 '06 #3

P: n/a
am******@gmail.com wrote:
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!

Nov 15 '06 #4

P: n/a

Dilip wrote:
am******@gmail.com wrote:
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!
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.

Nov 15 '06 #5

P: n/a
am******@gmail.com wrote:
Dilip wrote:
>am******@gmail.com wrote:
>>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!

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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 15 '06 #6

P: n/a

Dilip wrote:
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;
}

Nov 15 '06 #7

P: n/a
Dilip wrote:
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 TFoo<Tmake_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.

--
Best regards,
Andrey Tarasevich
Nov 15 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.