Template instantiation conflict with function definition

  • Thread starter Alessandro [AkiRoss] Re
  • Start date
A

Alessandro [AkiRoss] Re

Hello there!
While developing a policy-based application, I tried a code like this
one (which is real code).

I'm using g++ 4.3.3, which gives me these errors:
templ_error.cpp: In function ‘int main()’:
templ_error.cpp:22: error: multiple parameters named ‘base’
templ_error.cpp:23: error: request for member ‘exec’ in ‘test’, which
is of non-class type ‘Test<Numeric<3>, Numeric<5> > ()(Numeric<3>,
Numeric<5>)’

But I can't understand where is the problem: it seems that g++ read my
declaration of a variable as a function definition. Please tell me if
I'm doing it wrong: I can't see the error.
Here's the code of templ_error.cpp

#include <iostream>

using namespace std;

template <typename A, typename B>
struct Test {
A a;
B b;
Test(A a_, B b_): a(a_), b(b_) {}
void exec() { cout << "Executed!" << a() << "-" << b() <<"\n"; }
};

template <int X>
struct Numeric {
int base;
Numeric(int base_): base(base_) {}
int operator()() { return base + X; }
};

int main() {
int base = 1;
Test<Numeric<3>, Numeric<5> > test(Numeric<3>(base), Numeric<5>
(base));
test.exec();
return 0;
}


ps. Also with g++ 4.1 gives errors: the only difference is that
"multiple parameters name" is not present as it has introduced in gcc
4.3.

Any help is appreciated
Thanks!

Regards
 
V

Victor Bazarov

Alessandro said:
Hello there!
While developing a policy-based application, I tried a code like this
one (which is real code).

I'm using g++ 4.3.3, which gives me these errors:
templ_error.cpp: In function ‘int main()’:
templ_error.cpp:22: error: multiple parameters named ‘base’
templ_error.cpp:23: error: request for member ‘exec’ in ‘test’, which
is of non-class type ‘Test<Numeric<3>, Numeric<5> > ()(Numeric<3>,
Numeric<5>)’

But I can't understand where is the problem: it seems that g++ read my
declaration of a variable as a function definition. Please tell me if
I'm doing it wrong: I can't see the error.
Here's the code of templ_error.cpp

#include <iostream>

using namespace std;

template <typename A, typename B>
struct Test {
A a;
B b;
Test(A a_, B b_): a(a_), b(b_) {}
void exec() { cout << "Executed!" << a() << "-" << b() <<"\n"; }
};

template <int X>
struct Numeric {
int base;
Numeric(int base_): base(base_) {}
int operator()() { return base + X; }
};

int main() {
int base = 1;
Test<Numeric<3>, Numeric<5> > test(Numeric<3>(base), Numeric<5>
(base));

The line above declares a function 'test' with two arguments, 'base' and
'base'. The first argument is of type 'Numeric<3>', the second is of
type 'Numeric<5>'. If you needed to construct an object 'test' of type
'Test<...>', then you need to move your parentheses a bit:

Test<Numeric<3>, Numeric<5> >
test( ( Numeric<3> ) base, ( Numeric<5> )base );

Notice that I put the types in parentheses making those C-style type
cast expressions.
test.exec();

This line in your original program tries to apply '.' to the function
type, and that's what the second error message is about. Pay attention
to what your compiler is telling you!
return 0;
}


ps. Also with g++ 4.1 gives errors: the only difference is that
"multiple parameters name" is not present as it has introduced in gcc
4.3.

V
 
J

James Kanze

While developing a policy-based application, I tried a code
like this one (which is real code).
I'm using g++ 4.3.3, which gives me these errors:
templ_error.cpp: In function ‘int main()’:
templ_error.cpp:22: error: multiple parameters named ‘base’
templ_error.cpp:23: error: request for member ‘exec’ in ‘test’, which
is of non-class type ‘Test<Numeric<3>, Numeric<5> > ()(Numeric<3>,
Numeric<5>)’
But I can't understand where is the problem: it seems that g++
read my declaration of a variable as a function definition.

That's because it is a function declaration.

[...]
int main() {
int base = 1;
Test<Numeric<3>, Numeric<5> > test(Numeric<3>(base), Numeric<5> (base));

This declares (not defines) a function taking a Numeric< 3 > and
a Numeric< 5 > as arguments, and returning a Test< Numeric< 3 >,
Numeric< 5 > >. If what you want is a variable of type Test<
Numeric< 3 >, Numeric< 5 > >, initialized with a Numeric< 3 >
and a Numeric< 5 >, you must do something to prevent the string
"Numeric< 3 >( base )" (and "Numeric< 5 >( base )") from being
taken as a declaration:

Test< Numeric< 3 >, Numeric< 5 > >
test( (Numeric< 3 >( base )),
(Numeric< 5 >( base )) ) ;

Note the extra parentheses: C++ syntax doesn't allow the
declaration of a function parameter to be in parentheses, so the
results can only be an expression.

The fundamental problem is that "Numeric< 3 >( base )" can be
parsed as either an expression or a declaration. In such cases,
the rules of the language say that if a declaration is legal, it
is parsed as a declaration. So you have to do something to
create a context where a declaration would not be legal. My
favorite solution is the above, because it is (I think) the most
universally applicable. Other solutions would be to put the
parentheses around the Numeric< 3 > (only works if there is
exactly one argument), or to use copy initialization (doesn't
work if the type being constructed doesn't support copy).

This is sometimes known as C++'s most embarassing parse.
Everyone gets caught out by it from time to time.
 
M

Michael Doubez

While developing a policy-based application, I tried a code
like this one (which is real code).
I'm using g++ 4.3.3, which gives me these errors:
templ_error.cpp: In function ‘int main()’:
templ_error.cpp:22: error: multiple parameters named ‘base’
templ_error.cpp:23: error: request for member ‘exec’ in ‘test’, which
is of non-class type ‘Test<Numeric<3>, Numeric<5> > ()(Numeric<3>,
Numeric<5>)’
But I can't understand where is the problem: it seems that g++
read my declaration of a variable as a function definition.

That's because it is a function declaration.

    [...]
int main() {
    int base = 1;
    Test<Numeric<3>, Numeric<5> > test(Numeric<3>(base), Numeric<5> (base));

This declares (not defines) a function taking a Numeric< 3 > and
a Numeric< 5 > as arguments, and returning a Test< Numeric< 3 >,
Numeric< 5 > >.   [snip]
Note the extra parentheses: C++ syntax doesn't allow the
declaration of a function parameter to be in parentheses, so the
results can only be an expression.

The fundamental problem is that "Numeric< 3 >( base )" can be
parsed as either an expression or a declaration.  In such cases,
the rules of the language say that if a declaration is legal, it
is parsed as a declaration.  So you have to do something to
create a context where a declaration would not be legal. [snip]
This is sometimes known as C++'s most embarrassing parse.
Everyone gets caught out by it from time to time.

And I thought it was the necessary space in nested template :)
list<vector<int>> -> list<vector<int> >
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top