Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / December 2004

Tip: Looking for answers? Try searching our database.

Object instantiation gets parsed as func.def.?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Agoston Bejo - 06 Dec 2004 21:54 GMT
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

//--------------------------------------------
Doug Harrison [MVP] - 06 Dec 2004 22:05 GMT
>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
[quoted text clipped - 28 lines]
>
>//--------------------------------------------

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.

Signature

Doug Harrison
Microsoft MVP - Visual C++


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.