Template instantiation conflict with function definition

Discussion in 'C++' started by Alessandro [AkiRoss] Re, May 13, 2009.

  1. 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>,

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

    Any help is appreciated

    Alessandro [AkiRoss] Re, May 13, 2009
    1. Advertisements

  2. 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.
    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!
    Victor Bazarov, May 13, 2009
    1. Advertisements

  3. Alessandro [AkiRoss] Re

    James Kanze Guest

    That's because it is a function declaration.

    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.
    James Kanze, May 13, 2009
  4. And I thought it was the necessary space in nested template :)
    list<vector<int>> -> list<vector<int> >
    Michael Doubez, May 13, 2009
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.