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 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
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
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
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
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.);
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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>()
|
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)...
|
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...
|
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&);
...
|
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,...
| |
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...
|
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...
|
by: MurphyII |
last post by:
Just a little sample :
class A
{
public:
A( )
{
}
template<typename T> A( const typename T& a)
{
|
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 ) :...
|
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...
|
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,...
| |
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,...
|
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...
|
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...
|
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...
|
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,...
|
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: 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 ...
|
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...
| |