Template parameter method being identified as a variable

A

Alex

The following simplified code reproduces my problem:

#include <iostream>

struct MyTypeHolder
{
typedef int MyType;
};

struct MyOp
{
template<class T>
typename T::MyType doOp(typename T::MyType t)
{
return t + 1;
}
};

template <class Op>
class OpUser
{
public:
int getIntValue(int i)
{
return op_.doOp<MyTypeHolder>(i); // Line 23
}

private:
Op op_;
};

int main()
{
OpUser<MyOp> op_user;

std::cout << op_user.getIntValue(1) << std::endl;

return 0;
}


This compiles and works with VS.Net 2003 but not with g++ 3.4.3, which
I am assuming is following the standard more strictly.

The errors reported by g++ are:

test.cpp: In member function `int OpUser<Op>::getIntValue(int)':
test.cpp:23: error: expected primary-expression before '>' token

It seems to be treating the opening '<' character as a less than
rather than the start of an invocation of an explicit template method.
I don't really have control over the MyTypeHolder or MyOp classes, but
I can make changes to the OpUser class.

Does anyone know of a suitable fix for my problem?

Thanks,

Alex
 
A

Alf P. Steinbach

* Alex:
The following simplified code reproduces my problem:

#include <iostream>

struct MyTypeHolder
{
typedef int MyType;
};

struct MyOp
{
template<class T>
typename T::MyType doOp(typename T::MyType t)
{
return t + 1;
}
};

template <class Op>
class OpUser
{
public:
int getIntValue(int i)
{
return op_.doOp<MyTypeHolder>(i); // Line 23
}

private:
Op op_;
};

int main()
{
OpUser<MyOp> op_user;

std::cout << op_user.getIntValue(1) << std::endl;

return 0;
}


This compiles and works with VS.Net 2003 but not with g++ 3.4.3, which
I am assuming is following the standard more strictly.

The errors reported by g++ are:

test.cpp: In member function `int OpUser<Op>::getIntValue(int)':
test.cpp:23: error: expected primary-expression before '>' token

It seems to be treating the opening '<' character as a less than
rather than the start of an invocation of an explicit template method.
I don't really have control over the MyTypeHolder or MyOp classes, but
I can make changes to the OpUser class.

Does anyone know of a suitable fix for my problem?

return op_.template doOp<MyTypeHolder>(i); // Line 23
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top