template constructor

  • Thread starter Alexander Stippler
  • Start date
A

Alexander Stippler

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
 
M

msalters

Alexander said:
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::eek:perator()(double).

Regards,
Michiel Salters
 
M

msalters

Alexander said:
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::eek:perator()(double).

Regards,
Michiel Salters
 
M

msalters

Alexander said:
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::eek:perator()(double).

Regards,
Michiel Salters
 
M

msalters

Alexander said:
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::eek:perator()(double).

Regards,
Michiel Salters
 
T

Tom Widmer

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 said:
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
 
A

Alexander Stippler

msalters said:
Nothing, it seems.


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::eek:perator()(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.);
 
A

Alexander Stippler

Tom said:
That's the declaration of a function "p" that takes a pointer to a


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
 
V

Victor Bazarov

Alexander said:
Tom Widmer wrote:




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
 

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

No members online now.

Forum statistics

Threads
474,184
Messages
2,570,978
Members
47,561
Latest member
gjsign

Latest Threads

Top