473,503 Members | 2,238 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_function< T, size_t,
double >
{
typename ABinaryFunction::result_type
operator()( typename ABinaryFunction< T >::first_argument_type lhs,
typename ABinaryFunction< T >::second_argument_type
rhs )
{
return ABinaryFunction< T >::result_type();
}
};

template< typename T >
class AClass
{
public:

// constructor takes one specialized binary_function type parameter.
AClass( std::binary_function< 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()(ABinaryFunction<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 1516
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( (ABinaryFunction<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)

iEYEABECAAYFAkh9QQQACgkQx9p3GYHlUOKAngCcDHSHPWH8ZJ XoG/hkolJnhxRi
U6cAn0YdD8w/dcTuyaAIj0CL6NjR4ips
=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_function< T, size_t,
double >
{
* typename ABinaryFunction::result_type
* operator()( typename ABinaryFunction< T >::first_argument_type lhs,
* * * * * * * typename ABinaryFunction< T >::second_argument_type
rhs )
* {
* * return ABinaryFunction< T >::result_type();
* }

};

template< typename T >
class AClass
{
* public:

* // constructor takes one specialized binary_function type parameter.
* AClass( std::binary_function< 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()(ABinaryFunction<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 "constructor by assignment" as follows:

AClass< int instance_a = AClass< int >( ABinaryFunction< int
>() );
I know the "constructor 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( (ABinaryFunction<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_pgp-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 "constructor 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
3057
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...
23
5139
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
2970
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...
24
3709
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
1874
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...
6
2443
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...
74
15868
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...
12
7173
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? ...
3
2427
by: mhvaughn | last post by:
struct S1 { int i; }; struct S2 { S1 s; // version 1 S2() {} ; // version 2
0
7207
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
7093
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...
1
7012
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
7468
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
4690
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...
0
3171
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1522
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 ...
1
748
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
402
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...

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.