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

Object instantiation gets parsed as func.def.?

Hi! This is the smallest example I was able to create to demonstratet the
problem. It seems that a certain kind of object instantiation gets parsed as
a function definition (or whatever) by the compiler. I don't know if it
comes from some (maybe not-so-)dark corner of the C++ standard, or VC7.1 has
got a bug.

Thx,
Agoston

//-----------------------------------
template<typename T>
struct A {
A(T t):t(t) {}
T t;
};

template<typename T> void f()
{
cout << T() << endl;
A<T> a1((T()));
A<T> a2(T()); // function definition?
cout << a1.t << endl;
cout << a2.t << endl; // compile error! (*)
}

int _tmain(int argc, _TCHAR* argv[])
{
f<int>();
return 0;
}

// (*) error message:
// error C2228: left of '.t' must have class/struct/union type
// type is 'overloaded-function'
// see reference to function template instantiation
// 'void f<int>(void)' being compiled

//--------------------------------------------
Nov 17 '05 #1
1 954
Agoston Bejo wrote:
Hi! This is the smallest example I was able to create to demonstratet the
problem. It seems that a certain kind of object instantiation gets parsed as
a function definition (or whatever) by the compiler. I don't know if it
comes from some (maybe not-so-)dark corner of the C++ standard, or VC7.1 has
got a bug.
The "dark corner" theory is correct.
Thx,
Agoston

//-----------------------------------
template<typename T>
struct A {
A(T t):t(t) {}
T t;
};

template<typename T> void f()
{
cout << T() << endl;
A<T> a1((T()));
A<T> a2(T()); // function definition?
cout << a1.t << endl;
cout << a2.t << endl; // compile error! (*)
}

int _tmain(int argc, _TCHAR* argv[])
{
f<int>();
return 0;
}

// (*) error message:
// error C2228: left of '.t' must have class/struct/union type
// type is 'overloaded-function'
// see reference to function template instantiation
// 'void f<int>(void)' being compiled

//--------------------------------------------


VC is correct. This is what Scott Meyers described as a "vexing parse" in
"Effective STL". To simplify:

int f(int());

This declares a function f that has one parameter of the type:

int (*)();

That is, the parameter is unnamed and has type "pointer to a function that
has no parameters and returns int".

You've already discovered one way to get the effect you're after, namely
parenthesizing the ctor argument.

--
Doug Harrison
Microsoft MVP - Visual C++
Nov 17 '05 #2

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

Similar topics

7
by: johny smith | last post by:
Based on my experience there is two ways to instantiate an object. Method 1: Car* car1 = new Car();
13
by: Jake Barnes | last post by:
I saw this sentence: "The last stage of variable instantiation is to create named properties of the Variable object that correspond with all the local variables declared within the function." ...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: greek_bill | last post by:
Hi, I have a template function for which I use SFINAE to restrict one of the parameters. Then I also have a partial specialization of this function.I would like to provide an explicit...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.