473,799 Members | 2,837 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Constructor syntax woes.

Greetings all,

I have run into a small problem with my understanding of some C++
language syntax, and seek some clarification.

Below is a condensed version of some code I'm having difficulty with:

=============== =============== =============== =============== ===

#include <cstddef// size_t
#include <functional// binary_function

// the input parameter type to a class constructor.
template< typename T >
struct ABinaryFunction : public std::binary_fun ction< T, size_t,
double >
{
typename ABinaryFunction ::result_type
operator()( typename ABinaryFunction < T >::first_argume nt_type lhs,
typename ABinaryFunction < T >::second_argum ent_type
rhs )
{
return ABinaryFunction < T >::result_type( );
}
};

template< typename T >
class AClass
{
public:

// constructor takes one specialized binary_function type parameter.
AClass( std::binary_fun ction< T, size_t, double function )
{
}

// arbitrary method to test instantiation.
bool True()
{
return true;
}
};

int main()
{
// this won't create a class of type "AClass".
AClass< int instance_a( ABinaryFunction < int >() );
// this will not compile.
bool bool_a = instance_a.True ();

// this will create a class of type "AClass", along with an unwanted
binary_function _b.
ABinaryFunction < int binary_function _b;
AClass< int instance_b( binary_function _b );
// this will compile.
bool bool_b = instance_b.True ();

return 0;
}

=============== =============== =============== =============== ===

I'm using gcc-4.3, and get the following compile-error:

test.cpp:37: error: request for member ‘True’ in ‘instance_a’, which
is of non-class type ‘AClass<int()(A BinaryFunction< int(*)())'

I'm not very adept at deciphering uncommon C++ syntax, but my best
guess is that line 37 is interpreted as a definition or declaration of
the parenthesis operator, which takes a pointer to ABinaryFunction 's
parenthesis operator, and returns AClass. This is not what I expected
or intended at all.

What I wish to know is: why does the first stanza in main not
compile? To me, it is exactly the same as the second stanza; what am
I missing?

The second stanza works, so I can get by. However, using an anonymous
temporary ABinaryFunction object inline, as I intended stanza one to
be, is cleaner / more intuitive. Additionally, I'd like to know what
the syntax should be for what I intended, ( assuming it's possible. )

Thanks for your consideration,

-- Charles Wilcox
Jul 15 '08 #1
3 1530
Sam
wi***@cynd.net writes:
[ snippety ]

I'm not very adept at deciphering uncommon C++ syntax, but my best
guess is that line 37 is interpreted as a definition or declaration of
the parenthesis operator, which takes a pointer to ABinaryFunction 's
parenthesis operator, and returns AClass. This is not what I expected
or intended at all.
No, it appears to be parsed as a function prototype, that's what appears to
be happening. Consider the following statement:

int foo (char () );

This gets parsed as a prototype of a function that returns an int, and takes
a parameter that's a pointer to a function that returns a char, and takes
no parameters.

Your declaration is:

AClass< int instance_a( ABinaryFunction <int>() );

This apparently gets parsed a function prototype: a prototype for a function
that returns an AClass<int>, and that takes an argument of a pointer to a
function that returns an ABinaryFunction <int>, and takes no arguments.

When templates are involved, weird parsing anomalies like this are quite
common. I'm sure there's some obscure clause in the C++ standard that
explains why this gets parsed this way, but that's an academic excersize. I
note that if you change this to:

AClass< int instance_a( (ABinaryFunctio n<int>()) );

This apparently does what you want: invoke the default constructor for
ABinaryFunction <int>, and pass the result as the argument to AClass<int>'s
constructor.

Heh, this is a nice one.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEABECAAYFAkh 9QQQACgkQx9p3GY HlUOKAngCcDHSHP WH8ZJXoG/hkolJnhxRi
U6cAn0YdD8w/dcTuyaAIj0CL6Nj R4ips
=NWMO
-----END PGP SIGNATURE-----

Jul 16 '08 #2
On Jul 15, 7:21*pm, wi...@cynd.net wrote:
Greetings all,

I have run into a small problem with my understanding of some C++
language syntax, and seek some clarification.

Below is a condensed version of some code I'm having difficulty with:

=============== =============== =============== =============== ===

#include <cstddef// size_t
#include <functional// binary_function

// the input parameter type to a class constructor.
template< typename T >
struct ABinaryFunction : public std::binary_fun ction< T, size_t,
double >
{
* typename ABinaryFunction ::result_type
* operator()( typename ABinaryFunction < T >::first_argume nt_type lhs,
* * * * * * * typename ABinaryFunction < T >::second_argum ent_type
rhs )
* {
* * return ABinaryFunction < T >::result_type( );
* }

};

template< typename T >
class AClass
{
* public:

* // constructor takes one specialized binary_function type parameter.
* AClass( std::binary_fun ction< T, size_t, double function )
* {
* }

* // arbitrary method to test instantiation.
* bool True()
* {
* * return true;
* }

};

int main()
{
* // this won't create a class of type "AClass".
* AClass< int instance_a( ABinaryFunction < int >() );
* // this will not compile.
* bool bool_a = instance_a.True ();

* // this will create a class of type "AClass", along with an unwanted
binary_function _b.
* ABinaryFunction < int binary_function _b;
* AClass< int instance_b( binary_function _b );
* // this will compile.
* bool bool_b = instance_b.True ();

* return 0;

}

=============== =============== =============== =============== ===

I'm using gcc-4.3, and get the following compile-error:

test.cpp:37: error: request for member ‘True’ in ‘instance_a’, which
is of non-class type ‘AClass<int()(A BinaryFunction< int(*)())'

I'm not very adept at deciphering uncommon C++ syntax, but my best
guess is that line 37 is interpreted as a definition or declaration of
the parenthesis operator, which takes a pointer to ABinaryFunction 's
parenthesis operator, and returns AClass. *This is not what I expected
or intended at all.

What I wish to know is: why does the first stanza in main not
compile? *To me, it is exactly the same as the second stanza; what am
I missing?

The second stanza works, so I can get by. *However, using an anonymous
temporary ABinaryFunction object inline, as I intended stanza one to
be, is cleaner / more intuitive. *Additionally, I'd like to know what
the syntax should be for what I intended, ( assuming it's possible. )

Thanks for your consideration,

*-- Charles Wilcox
This evening I realized I could explicitly break the line into a
declaration and "constructo r by assignment" as follows:

AClass< int instance_a = AClass< int >( ABinaryFunction < int
>() );
I know the "constructo r by assignment" is a bit confusing to some, but
I know it's actually using the explicit constructor only, as I put
"operator=" into a "private" section.

I like it a bit more than stanza two; I'll use this potentially.
Jul 16 '08 #3
On Jul 15, 8:29*pm, Sam <s...@email-scan.comwrote:
wi...@cynd.net writes:
[ snippety ]
I'm not very adept at deciphering uncommon C++ syntax, but my best
guess is that line 37 is interpreted as a definition or declaration of
the parenthesis operator, which takes a pointer to ABinaryFunction 's
parenthesis operator, and returns AClass. *This is not what I expected
or intended at all.

No, it appears to be parsed as a function prototype, that's what appears to
be happening. Consider the following statement:

int foo (char () );

This gets parsed as a prototype of a function that returns an int, and takes
a parameter that's a pointer to a function that returns a char, and takes
no parameters.

Your declaration is:

AClass< int instance_a( ABinaryFunction <int>() *);

This apparently gets parsed a function prototype: a prototype for a function
that returns an AClass<int>, and that takes an argument of a pointer to a
function that returns an ABinaryFunction <int>, and takes no arguments.

When templates are involved, weird parsing anomalies like this are quite
common. I'm sure there's some obscure clause in the C++ standard that
explains why this gets parsed this way, but that's an academic excersize.I
note that if you change this to:

AClass< int instance_a( (ABinaryFunctio n<int>()) );

This apparently does what you want: invoke the default constructor for
ABinaryFunction <int>, and pass the result as the argument to AClass<int>'s
constructor.

Heh, this is a nice one.

*application_pg p-signature_part
1KDownload
Sam,

Thanks for the input. I see your point, that it's declaring a
function prototype.

The double-parens trick is very cute, although it's not very clear /
why/ it works. It's a bit subtle... I think I'd almost prefer
something more explicit. As I just previously posted, I was able to
make the line compile by breaking it out into a variable declaration,
and a "constructo r by assignment". Of course, that could confuse
people into thinking a real assignment is happening.

Ahh well, the fun of C++ parsing legacy.

-- Charles Wilcox
Jul 16 '08 #4

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

Similar topics

34
3116
by: Andy | last post by:
1) Is there any use of defining a class with a single constructor declared in private scope? I am not asking a about private copy constructors to always force pass/return by reference. 2) Is this in any way used to create singletons. Can someone say how? Cheers, Andy
23
5186
by: Fabian Müller | last post by:
Hi all, my question is as follows: If have a class X and a class Y derived from X. Constructor of X is X(param1, param2) . Constructor of Y is Y(param1, ..., param4) .
18
3004
by: Matt | last post by:
I try to compare the default constructor in Java and C++. In C++, a default constructor has one of the two meansings 1) a constructor has ZERO parameter Student() { //etc... } 2) a constructor that all parameters have default values
24
3786
by: slurper | last post by:
i have the following class sequence { public: sequence (const sequence& mysequence, const int newjob) { job_sequence(mysequence.job_sequence) job_sequence.push_back(newjob); ... }
4
1897
by: Dan Stromberg | last post by:
Hi folks. I'm working on building some software, some of which is written in C++, for a researcher here at the University. I have an extensive background in C and python, but I haven't done much with C++ - I kind of mostly abandoned C++ some time ago, when I coded a project in C++, and some of my coworkers refused to use it -because- it was in C++.
6
2468
by: daveb | last post by:
I'm trying to write some code that calls the constructors of STL containers explicitly, and I can't get it to compile. A sample program is below. One compiler complains about the last two lines (the map constructor calls), saying that I'm trying to take the address of a constructor. Another compiler complains about all four of the last lines, just saying "invalid use of class". What is the correct syntax for calling a container...
74
16038
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the creation of this implicit default constructor, to force the creation of a struct via my constructor only? Zytan
12
7217
by: Rahul | last post by:
Hi Everyone, I have the following code and i'm able to invoke the destructor explicitly but not the constructor. and i get a compile time error when i invoke the constructor, why is this so? class Trial { public: Trial() {
3
2446
by: mhvaughn | last post by:
struct S1 { int i; }; struct S2 { S1 s; // version 1 S2() {} ; // version 2
0
10482
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10251
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
10225
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
9072
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...
1
7564
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6805
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4139
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
2
3759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.