Compilation Problem

P

Peter A. Buhr

I cannot get the following code fragment to compile:

template<typename T1> struct X {
typedef void (X<T1>::* pmem)( int i ); // type for pointer to member routine
template<typename T2> void mem( int i ) { // create template member routine
pmem p = &X<T1>::mem<T2>; // create and initialize member pointer
}
};
int main() {
X<int> x;
x.mem<int>( 3 );
}

with gcc version 4.2.4 (Ubuntu 4.2.4-1ubuntu3). I get these errors:

g++ test2.cc
test2.cc: In member function 'void X<T1>::mem(int)':
test2.cc:4: error: expected primary-expression before '>' token
test2.cc:4: error: expected primary-expression before ';' token

So am I:

1. attempting to do something that is invalid in the language?

2. incorrectly specifying the syntax for the address of the member routine?

3. attempting to do something valid but currently unimplemented by the
specified version of gcc?

Any help would be appreciated.
 
R

red floyd

I cannot get the following code fragment to compile:

template<typename T1> struct X {
    typedef void (X<T1>::* pmem)( int i ); // type for pointer to member routine
    template<typename T2> void mem( int i ) { // create template member routine
        pmem p = &X<T1>::mem<T2>;       // create and initialize member pointer

Try this:
 
B

Bart van Ingen Schenau

Peter said:
I cannot get the following code fragment to compile:

template<typename T1> struct X {
typedef void (X<T1>::* pmem)( int i ); // type for pointer to
member routine template<typename T2> void mem( int i ) { // create
template member routine
pmem p = &X<T1>::mem<T2>; // create and initialize member pointer

When accessing members of a template for which the exact template
arguments are not yet known (such as X above), the compiler needs a bit
of help to resolve ambiguities in the C++ syntax. Especially in
identifying member types and member templates.
In this case, the compiler needs to be told that 'mem<T2>' refers to a
template:
pmem p = &X said:
}
};
int main() {
X<int> x;
x.mem<int>( 3 );
}

with gcc version 4.2.4 (Ubuntu 4.2.4-1ubuntu3). I get these errors:

g++ test2.cc
test2.cc: In member function 'void X<T1>::mem(int)':
test2.cc:4: error: expected primary-expression before '>' token
test2.cc:4: error: expected primary-expression before ';' token

So am I:

1. attempting to do something that is invalid in the language?
No.


2. incorrectly specifying the syntax for the address of the member
routine?
No.

3. attempting to do something valid but currently unimplemented by the
specified version of gcc?

No.

4. using the wrong syntax to do what I want?
Yes.
Any help would be appreciated.

Bart v Ingen Schenau
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top