473,799 Members | 3,224 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<typena me 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 1498

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 "declaratio n" 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.c omwrote:
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.c omwrote:
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(hellowor ld);

is treated as a "declaratio n" 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
1918
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
2320
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 same type for a given instance of the class, but different types for different instances.) #include<set> using namespace std; template<class T>
5
4246
by: kalita | last post by:
template<class T> class A { template<class Y> A(const A<Y> &) { // whatever } };
6
1528
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 constructor other than the default constructor. I couldn't figure out why it wasn't working so I changed the member variable to a pointer to an object instead and intialized it in the constructor and destroyed in the destructor. It bothered me that...
7
1730
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, short Trank, bool Tcont> TopTen<Tnum,Trank,Tcont>::TopTen( const TopTen<Tnum,Trank,Tcont& tten );
3
12174
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 <class T> TestClass()
1
2428
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 it i have the templated linked list class and i wrote all the constructor and the destructor and other thing for the bank account class but the problem is that i don't have any idea how to link the templated linkedlist with the bankaccount class...
2
2290
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 <stack> template <class T> class A { public:
5
4025
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 of all specialisations of the class. I did that (or thought I did) but the code I produced compiles with gcc 3.4.2 but not with Microsoft Visual Studio 2008 (Express Edition). So I have reduced my code to a minimum compilable example that exhibits...
0
9685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9538
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10247
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10214
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10023
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9067
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5459
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4135
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 we have to send another system
3
2935
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.