legal C++ code (use of templates with parameters)?

A

atai

A question on C++:

Is the following legal C++ code?


typedef int (*func)(int);

template <class A>
class D1
{
public:
template <class B>
func f(void)
{
return A::some_func<B>;
}
};

template <class A1, class B1>
func g(void)
{

return A1::m1<B1>;
}


g++ (4.4.1) would not compile this code, with error

/tmp/t.cpp: In member function ‘int (* D1<A>::f())(int)’:
/tmp/t.cpp:12: error: expected primary-expression before ‘>’ token
/tmp/t.cpp:12: error: expected primary-expression before ‘;’ token
/tmp/t.cpp: In function ‘int (* g())(int)’:
/tmp/t.cpp:20: error: expected primary-expression before ‘>’ token
/tmp/t.cpp:20: error: expected primary-expression before ‘;’ token

but it seems to be something that should be legal.

Thanks for any answer on this
 
A

Alf P. Steinbach /Usenet

* (e-mail address removed), on 20.07.2010 03:11:
A question on C++:

Is the following legal C++ code?


typedef int (*func)(int);

template<class A>
class D1
{
public:
template<class B>
func f(void)
{
return A::some_func<B>;
}
}; [snip]

g++ (4.4.1) would not compile this code, with error

/tmp/t.cpp: In member function ‘int (* D1<A>::f())(int)’:
/tmp/t.cpp:12: error: expected primary-expression before ‘>’ token
/tmp/t.cpp:12: error: expected primary-expression before ‘;’ token
/tmp/t.cpp: In function ‘int (* g())(int)’:
/tmp/t.cpp:20: error: expected primary-expression before ‘>’ token
/tmp/t.cpp:20: error: expected primary-expression before ‘;’ token

but it seems to be something that should be legal.

Try to stick a 'template' keyword in there:

A::template some_func<B>;

Disclaimer: haven't tried to compile your code.


Cheers & hth.,

- Alf
 
A

Andrey Tarasevich

A question on C++:

Is the following legal C++ code?


typedef int (*func)(int);

template <class A>
class D1
{
public:
template <class B>
func f(void)
{
return A::some_func<B>;
}
};

`some_func` appears to be a template member of a depenedent type `A`,
which means that any references to `some_func` in this context should be
prepended with the `template` keyword

...
template <class B>
func f(void)
{
return A::template some_func<B>;
}
...
template <class A1, class B1>
func g(void)
{

return A1::m1<B1>;
}

Exactly the same problem with member `m1` in this case as well

template <class A1, class B1>
func g(void)
{
return A1::template m1<B1>;
}
 
J

James Kanze

A question on C++:
Is the following legal C++ code?

No. And since it's not really clear to me what it is meant to
do, it's hard to say what changes would be necessary to make it
legal.
typedef int (*func)(int);
template <class A>
class D1
{
public:
template <class B>
func f(void)
{
return A::some_func<B>;

What is A::some_func? I don't know, and neither does the
compiler. If it is supposed to be a member function template of
A, then 1) as others have pointed out, you need to add a keyword
template, and 2) the only things you can do with such member
functions is call them or take their address, and you do
neither.

return &template A::some_func<B>;

would be legal (I think), *if* A contained a static member
function named some_func, which took an int as argument, and
returned an int.
template <class A1, class B1>
func g(void)
{
return A1::m1<B1>;
}

The same issues as above. You have to tell the compiler that
A1::m1 is a template. (Otherwise, it assumes that the '<'
following it is a less-than operator.) And then you don't do
anything legal with the member function (assuming it is a member
function).
 
A

Andrey Tarasevich

James said:
What is A::some_func? I don't know, and neither does the
compiler. If it is supposed to be a member function template of
A, then 1) as others have pointed out, you need to add a keyword
template, and 2) the only things you can do with such member
functions is call them or take their address, and you do
neither.

return &template A::some_func<B>;

would be legal (I think), *if* A contained a static member
function named some_func, which took an int as argument, and
returned an int.

Firstly, the syntax for the extra `template` keyword for a member
template is different from the syntax for the extra `typename` keyword
for a member type name. `typename` is placed in from of the entire name,
while `template` is placed "in the middle", right before the "short"
name of the member

return &A::template some_func<B>;

Secondly, as you correctly noted, `some_func` should be a template for a
_static_ member function. However, the function-to-pointer conversion
applies to static member functions the same way it applies to "ordinary"
functions, meaning that the explicit application of unary `&` can be
omitted. I.e. if the above is well-formed, then

return A::template some_func<B>;

is well-formed as well.
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top