473,513 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

template constructor

Hello,

short question: What is illegal about the following code?

template <typename T>
class Method
{
};

class Procedure
{
public:
template <typename T>
Procedure(const Method<T> &rhs);

double
operator()(double x);
};

int
main()
{
Procedure p(Method<double>());
p(1.);

return 0;
}

I want the operator() to be called with p(1.), but the compiler thinks
different and wants to choose the constructor? Why?

regards,
alex
Jul 22 '05 #1
8 2232

Alexander Stippler wrote:
Hello,

short question: What is illegal about the following code?
Nothing, it seems.
template <typename T>
class Method
{
};

class Procedure
{
public:
template <typename T>
Procedure(const Method<T> &rhs);

double
operator()(double x);
};

int
main()
{
Procedure p(Method<double>());
p(1.);

return 0;
}

I want the operator() to be called with p(1.), but the compiler thinks different and wants to choose the constructor? Why?


No, it doesn't think that. It /does/ call the
Procedure::Procedure(Method<double>() const& rhs) constructor
first, because that is the argument you provided when you
defined p. Oviously, you must create p before you use p.

Once it is constructed (the ctor returned) the compiler /will/
call Procedure::operator()(double).

Regards,
Michiel Salters

Jul 22 '05 #2
Alexander Stippler wrote:
Hello,

short question: What is illegal about the following code?
Nothing, it seems.
template <typename T>
class Method
{
};

class Procedure
{
public:
template <typename T>
Procedure(const Method<T> &rhs);

double
operator()(double x);
};

int
main()
{
Procedure p(Method<double>());
p(1.);

return 0;
}

I want the operator() to be called with p(1.), but the compiler thinks different and wants to choose the constructor? Why?


No, it doesn't think that. It /does/ call the
Procedure::Procedure(Method<double>() const& rhs) constructor
first, because that is the argument you provided when you
defined p. Oviously, you must create p before you use p.

Once it is constructed (the ctor returned) the compiler /will/
call Procedure::operator()(double).

Regards,
Michiel Salters

Jul 22 '05 #3
Alexander Stippler wrote:
Hello,

short question: What is illegal about the following code?
Nothing, it seems.
template <typename T>
class Method
{
};

class Procedure
{
public:
template <typename T>
Procedure(const Method<T> &rhs);

double
operator()(double x);
};

int
main()
{
Procedure p(Method<double>());
p(1.);

return 0;
}

I want the operator() to be called with p(1.), but the compiler thinks different and wants to choose the constructor? Why?


No, it doesn't think that. It /does/ call the
Procedure::Procedure(Method<double>() const& rhs) constructor
first, because that is the argument you provided when you
defined p. Oviously, you must create p before you use p.

Once it is constructed (the ctor returned) the compiler /will/
call Procedure::operator()(double).

Regards,
Michiel Salters

Jul 22 '05 #4
Alexander Stippler wrote:
Hello,

short question: What is illegal about the following code?
Nothing, it seems.
template <typename T>
class Method
{
};

class Procedure
{
public:
template <typename T>
Procedure(const Method<T> &rhs);

double
operator()(double x);
};

int
main()
{
Procedure p(Method<double>());
p(1.);

return 0;
}

I want the operator() to be called with p(1.), but the compiler thinks different and wants to choose the constructor? Why?


No, it doesn't think that. It /does/ call the
Procedure::Procedure(Method<double>() const& rhs) constructor
first, because that is the argument you provided when you
defined p. Oviously, you must create p before you use p.

Once it is constructed (the ctor returned) the compiler /will/
call Procedure::operator()(double).

Regards,
Michiel Salters

Jul 22 '05 #5
msalters wrote:
Alexander Stippler wrote:
Hello,

short question: What is illegal about the following code?


Nothing, it seems.
template <typename T>
class Method
{
};

class Procedure
{
public:
template <typename T>
Procedure(const Method<T> &rhs);

double
operator()(double x);
};

int
main()
{
Procedure p(Method<double>());
p(1.);

return 0;
}

I want the operator() to be called with p(1.), but the compiler

thinks
different and wants to choose the constructor? Why?


No, it doesn't think that. It /does/ call the
Procedure::Procedure(Method<double>() const& rhs) constructor
first, because that is the argument you provided when you
defined p. Oviously, you must create p before you use p.

Once it is constructed (the ctor returned) the compiler /will/
call Procedure::operator()(double).

Regards,
Michiel Salters


But icc8.1, gcc3.4 and como complain like that:

example.cc(20): error: argument of type "double" is incompatible with
parameter of type "Method<double> (*)()"
p(1.);

Jul 22 '05 #6
On Tue, 07 Dec 2004 15:13:50 +0100, Alexander Stippler
<st**@mathematik.uni-ulm.de> wrote:
Hello,

short question: What is illegal about the following code?

template <typename T>
class Method
{
};

class Procedure
{
public:
template <typename T>
Procedure(const Method<T> &rhs);

double
operator()(double x);
};

int
main()
{
Procedure p(Method<double>());
That's the declaration of a function "p" that takes a pointer to a
function that returns a Method<double> and returns a Procedure.
p(1.);

return 0;
}

I want the operator() to be called with p(1.), but the compiler thinks
different and wants to choose the constructor? Why?


Because it thinks p(1.) is a call of the (undefined) function you
declared above. I think you meant:

Procedure p((Method<double>()));
or the semantically very slightly different:
Procedure p = Method<double>();

Tom
Jul 22 '05 #7
Tom Widmer wrote:
On Tue, 07 Dec 2004 15:13:50 +0100, Alexander Stippler
<st**@mathematik.uni-ulm.de> wrote:
Hello,

short question: What is illegal about the following code?

template <typename T>
class Method
{
};

class Procedure
{
public:
template <typename T>
Procedure(const Method<T> &rhs);

double
operator()(double x);
};

int
main()
{
Procedure p(Method<double>());


That's the declaration of a function "p" that takes a pointer to a
function that returns a Method<double> and returns a Procedure.
p(1.);

return 0;
}

I want the operator() to be called with p(1.), but the compiler thinks
different and wants to choose the constructor? Why?


Because it thinks p(1.) is a call of the (undefined) function you
declared above. I think you meant:

Procedure p((Method<double>()));
or the semantically very slightly different:
Procedure p = Method<double>();

Tom


Can you please explain to me the effect of the additional ()-pair? Where can
I find this difference in the standard?

regards,
alex
Jul 22 '05 #8
Alexander Stippler wrote:
Tom Widmer wrote:

On Tue, 07 Dec 2004 15:13:50 +0100, Alexander Stippler
<st**@mathematik.uni-ulm.de> wrote:

Hello,

short question: What is illegal about the following code?

template <typename T>
class Method
{
};

class Procedure
{
public:
template <typename T>
Procedure(const Method<T> &rhs);

double
operator()(double x);
};

int
main()
{
Procedure p(Method<double>());


That's the declaration of a function "p" that takes a pointer to a
function that returns a Method<double> and returns a Procedure.

p(1.);

return 0;
}

I want the operator() to be called with p(1.), but the compiler thinks
different and wants to choose the constructor? Why?


Because it thinks p(1.) is a call of the (undefined) function you
declared above. I think you meant:

Procedure p((Method<double>()));
or the semantically very slightly different:
Procedure p = Method<double>();

Tom

Can you please explain to me the effect of the additional ()-pair? Where can
I find this difference in the standard?


How many times?

The statement:

<type-id> <identifier> ( <some-other-type-id> ( ) ) ;

is a _function_declaration_, not an object definition. Read the FAQ.

V
Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3908
by: Sebastian Faust | last post by:
Hi, I have 4 questions related to templates. I wanna do something like the following: template<typename T> class Template { public: Template_Test<T>()
3
2063
by: jack | last post by:
Hi there, I have a function F(x, y, z) and I want to calculate f(a) + f(b), where f(x) = F(x, x, z0) z0 is fixed. Suppose somebody wrote a routine called "Compute" which simply computes f(a)...
1
4479
by: Adam Dziendziel | last post by:
Hi all! I'm writing a luabind/boost::python-like binding utility for a Squirrel language to generating wrapper-functions for C++ classes and have a problem with passing the pointer-to-member...
14
2866
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&); ...
8
3135
by: Tony Johansson | last post by:
Hello Experts! What does this mean actually. If you have a template with a type and non-type template argument, say, like this template<typename T, int a> class Array {. . .}; then A<int,...
3
463
by: sandeep_kumar | last post by:
Hi, This one is related to template specialization. In the class "convert" I have defined one specialized constructor for "const char *" and one template constructor. The template constructor...
12
2597
by: mlimber | last post by:
This is a repost (with slight modifications) from comp.lang.c++.moderated in an effort to get some response. I am using Loki's Factory as presented in _Modern C++ Design_ for message passing in...
13
2900
by: MurphyII | last post by:
Just a little sample : class A { public: A( ) { } template<typename T> A( const typename T& a) {
4
1820
by: Deep | last post by:
Can I use a class in this manner, where a constructor is of templated: template<typename T> class my_class{ public: int x_; public: template<typename U> my_class(const my_class<U>& other ) :...
3
3557
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
7260
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
7384
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
7539
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...
1
7101
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7527
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
5686
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
4746
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...
0
1597
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
456
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.