473,396 Members | 1,864 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,396 software developers and data experts.

Templated constructor behaviour

Hi,

I have the following code:

#include <iostream>

int foo() {
std::cout << __func__ << std::endl;
}

struct func {
void operator()() {
std::cout << __func__ << std::endl;
}
};

class run {
private:
public:
run() {
std::cout << "THIS SHOULD NOT HAVE BEEN CALLED 1" << std::endl;
}
run(const run&) {
std::cout << "THIS SHOULD NOT HAVE BEEN CALLED 2" << std::endl;
}

template<typename Functor>
run(Functor f) {
std::cout << "CALLED" << std::endl;
f();
}
};

int main() {
run(foo);
run(func());
return 0;
}

My output is:
THIS SHOULD NOT HAVE BEEN CALLED 1

and clearly not what I expected. Any hints?

Thanks

Aug 28 '07 #1
6 1480

Neelesh Bodas wrote:

Look at the following example. Do you think it would compile and run?

class X { };
int main()
{
X(helloworld);
}

The above code compiles and runs properly. Why? because

X(helloworld);
Runs properly, although different to original intention...
is treated as a "declaration" by the compiler. ("anything that can be
treated as a declaration will be treated as a declaration"). Thus
Yes.
int main()
{
X(helloworld);
}

is same as saying
I think you meant "is not the same as saying"
int main()
{
X helloworld; //define variable of type X. The name of the
variable is "helloworld".
}
In the one case its a declaration of a function pointer, in the other
a definition of the variable. But I think that is what you meant, I'm
just clarifying it a bit for the OP.

Regards,

Werner

Aug 28 '07 #2
On Aug 28, 2:02 pm, werasm <wer...@gmail.comwrote:
Neelesh Bodas wrote:
int main()
{
X(helloworld);
}
is same as saying

I think you meant "is not the same as saying"
No i didnot.
int main()
{
X helloworld; //define variable of type X. The name of the
variable is "helloworld".
}
int the example given by me, I said that
int main() { X(helloworld);}
is semantically equivalent to (aka "same as" )
int main() { X helloworld;}

-N

Aug 28 '07 #3

Neelesh Bodas wrote:
the example given by me, I said that
int main() { X(helloworld);}
is semantically equivalent to (aka "same as" )
int main() { X helloworld;}
Ahhh, I learnt something. Your're right. I was thinking
about this case:

struct X
{
int foo();
};
int f1()
{
X helloworld;
helloworld.foo();
return 0;
}

int f2()
{
X helloworld();
helloworld(); //calls the function that returns X and takes void as
param
return 0;
}

Now for a question. Is this considered a definition? I know that it
is
categorized as a declaration.

X helloworld;

W

Aug 28 '07 #4
On Aug 28, 3:37 pm, werasm <wer...@gmail.comwrote:
Now for a question. Is this considered a definition?
I know that it ix categorized as a declaration.

X helloworld;
Yes, it is a definition. This statement defines an object of type X.
The fact that the constructor X::X() is invoked indicates that this is
a definition.

-N

Aug 28 '07 #5
Yes, it is a definition. This statement defines an object of type X.
The fact that the constructor X::X() is invoked indicates that this is
a definition.
This is exactly why your original wording...

"X(helloworld);

is treated as a "declaration" by the compiler. ("anything that can be
treated as a declaration will be treated as a declaration"). Thus ...
"

confused me.

Yes, its treated as a declaration, but a declaration <is adefinition
in this
case. All said, I've never used the syntax...

X(helloworld)

....before, and confused it with a function ptr returning X named
helloworld (which
is is not, of course).

Kind regards,

Werner

Aug 28 '07 #6
See Neelesh Boda's post about what you have actually declared in your main(), but to add
something further. You seem to think that there is a way to invoke a member function on
an object without actually creating an object. There isn't. Why have the run class at
all? Pull your templated function out of the class and invoke it directly. That is:

template <typename Functor>
void run(Functor f)
{
std::cout << "CALLED" << std::endl;
f();
}

int main()
{
run(foo);
run(func());
return 0;
}
Should work as you expect. Generally speaking, if you have a stateless class, think
twice about why you think you want a class. With the exception of functors, namespaces
or classes with static methods work just well (if not better) and you don't have to
create useless instances just to invoke the method.

joe
Aug 28 '07 #7

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

Similar topics

2
by: ferdinand.stefanus | last post by:
Hi, I have some questions regarding templated class constructor: #include <iostream> using namespace std; template<typename T> class Foo { public:
9
by: Jon Wilson | last post by:
I have a class which needs to accumulate data. The way we get this data is by calling a member function which returns float on a number of different objects of different type (they are all the...
5
by: kalita | last post by:
template<class T> class A { template<class Y> A(const A<Y> &) { // whatever } };
6
by: Dan Huantes | last post by:
I was presented a problem today where a class had member variable that was an object of a templated class. The class wanted to instantiate the object as a private member variable and call a...
7
by: Claudius | last post by:
Hello, in my class TopTen I need to define three constructors while only the last one, the most general in terms of templates, should be sufficient in my opinion: template <typename Tnum,...
3
by: Lawrence Spector | last post by:
How does one call a templated constructor inside of a class when instantiating an object? I made up a quick sample to demonstrate. #include <iostream> class TestClass { public: template...
1
by: eihabisaac | last post by:
hey everybody im having some trouble with my assignment we should write a program that will handle with a templated linkedlist for a bank account to add/delete amount to the account or sort...
2
card
by: card | last post by:
Hi everyone, I have a question about referencing a nested class contained within a templated class. Of course the best way to show you is by example. Here's my templated classes: #include...
5
Banfa
by: Banfa | last post by:
So I have a little problem, I have a template class and that class contains a template function; now what I want to do is declare that function in the class (or indeed the entire class) as a friend...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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
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,...

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.