W
willo
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
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