1

I'm trying to compile this code:

class P{
public:
    template<typename Color, typename It>
    void f(It a, It b, It c) const
    {cout << "in p\n";}
};

// If you uncomment these lines, it compiles!!!
//template<typename Color, typename It>
//void f(It a, It b, It c){}

template<typename P>
void g(const P& p)
{
// This line doesn't compile!!!
// Error
// =====
// error: expected primary-expression before ‘int’
//      p.f<int,int>(4,3,2);
//               ^
//
// error: expected ‘;’ before ‘int’
    p.f<int,int>(4,3,2);
}


int main(int argc, char** argv)
{
    const P p;
    p.f<int,int>(4,3,2);

    return 0; 
}

with g++: g++ -c -Wall -std=c++11 -pedantic

and I get the following error:

error: expected primary-expression before ‘int’
     p.f<int,int>(4,3,2);
              ^
error: expected ‘;’ before ‘int’

But if you uncomment the lines

//template<typename Color, typename It>
//void f(It a, It b, It c){}

it compiles. Why?

Thanks.

melpomene
  • 84,125
  • 8
  • 85
  • 148
Antonio
  • 579
  • 1
  • 3
  • 12
  • @NikolaBenes It is **not** a duplicate; the question is totally different. – lisyarus Aug 05 '16 at 17:45
  • It also compiles regardless of the usual and template arguments of the free function `f`, but it fails if it is not a template. Weeeeird. – lisyarus Aug 05 '16 at 17:47
  • Well, the problem here is that p (inside g) is a dependent name. The referenced question is about dependent names. – Nikola Benes Aug 05 '16 at 17:50

0 Answers0