473,413 Members | 1,833 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Template argument deduction question


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
7 1508
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

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

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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Dave | last post by:
Hello all, Consider this template: template <typename T> void foo(T bar) {...} Here are three ways to instantiate this: 1.
2
by: marco | last post by:
the problem: I use a typedef inside a class template, than I use this type (dim_v<N1>::Type) to define the argument of a template function f but when I call this function from main, the compiler...
3
by: BigMan | last post by:
Here is a piece of code: #include <memory> using namespace std; template< typename SomeType > void f(auto_ptr_ref< SomeType >) { }
1
by: Peng Yu | last post by:
Hi All, In the book Working Paper for Draft Proposed International Standard for Information Systems$)A!* Programming Language C+ +, template argument deduction is discussed. However, the...
14
by: Bart Samwel | last post by:
Hi everybody, I would really like some help explaining this apparent discrepancy, because I really don't get it. Here is the snippet: void foo(int&); void foo(int const&); ...
4
by: George | last post by:
Dear All, I'm compiling the code below with IBM's xlC 6.0 and get the message, "rmspace.cpp", line 34.48: 1540-0298 (S) Template argument deduction cannot be performed using the function...
2
by: coolpint | last post by:
Can anyone kindly provide an explanation as to why the compiler does not "see" the function template in the contrieved code below? I think the argument deduction fails but can't figure out...
7
by: gretean | last post by:
I have a problem that's driving me crazy involving Microsoft's ability to deduce template parameters. I am using Visual Studio .NET (aka VC7?), and it gives an error compiling the following code....
3
by: Fei Liu | last post by:
Hello, We all know that a template function can automatically deduce its parameter type and instantiate, e.g. template <tpyename T> void func(T a); func(0.f); This will cause func<floatto...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.