public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Template and member function pointers compile question
@ 2006-04-05 14:45 Tristan Bonsall
  2006-04-05 14:55 ` John Love-Jensen
  0 siblings, 1 reply; 4+ messages in thread
From: Tristan Bonsall @ 2006-04-05 14:45 UTC (permalink / raw)
  To: gcc-help

Hello,

I have a compile error trying to build come code with GCC. It works with 
MSVC 7, so I was hoping it was valid C++. What do I need to change to 
make this compile under GCC?

We are trying to use a pointer to a member function of a template class 
to speed up part of our code, where we are selecting one of a number of 
similar functions, so a template function makes the code easier to maintain.

This inane example reproduces the problem while retaining the structure 
of the original code.

//------------------------------------------------------------------------------

template<class T>
class TestClass
{
public:
                TestClass() {}

    void        setFunc(bool, bool, bool);

private:

    template<bool,bool,bool> void calculate();

protected:

    void        (TestClass::*pFunc)();

    float       data;
};

//------------------------------------------------------------------------------

template<class T>
template<bool PLUS, bool MODULO, bool SQUARE>
void TestClass<T>::calculate()
{
    if (PLUS)
    {
        data += 10;

        if (MODULO)
        {
            data %= 5;
        }
    }
    else
    {
        data -= 7;
    }

    if (SQUARE)
    {
        data *= data;
    }
}

//------------------------------------------------------------------------------

template<class T>
void TestClass<T>::setFunc(bool plus, bool modulo, bool square)
{
    if (plus)
    {
        if (modulo)
        {
            if (square)
            {
                pFunc = &TestClass<T>::calculate<true,true,true>;
            }
            else
            {
                pFunc = &TestClass<T>::calculate<true,true,false>;
            }
        }
        else
        {
            if (square)
            {
                pFunc = &TestClass<T>::calculate<true,false,true>;
            }
            else
            {
                pFunc = &TestClass<T>::calculate<true,false,false>;
            }
        }
    }
    else
    {
        if (modulo)
        {
            if (square)
            {
                pFunc = &TestClass<T>::calculate<false,true,true>;
            }
            else
            {
                pFunc = &TestClass<T>::calculate<false,true,false>;
            }
        }
        else
        {
            if (square)
            {
                pFunc = &TestClass<T>::calculate<false,false,true>;
            }
            else
            {
                pFunc = &TestClass<T>::calculate<false,false,false>;
            }
        }
    }
}

//------------------------------------------------------------------------------

int main()
{
    TestClass<int> testClass;

    testClass.setFunc(true, true, false);
}

//------------------------------------------------------------------------------

This gives the following error message when compiled.

testclass.cpp: In member function 'void TestClass<T>::setFunc(bool, 
bool, bool)':
testclass.cpp:57: error: expected primary-expression before ';' token
testclass.cpp:61: error: expected primary-expression before ';' token
testclass.cpp:68: error: expected primary-expression before ';' token
testclass.cpp:72: error: expected primary-expression before ';' token
testclass.cpp:82: error: expected primary-expression before ';' token
testclass.cpp:86: error: expected primary-expression before ';' token
testclass.cpp:93: error: expected primary-expression before ';' token
testclass.cpp:97: error: expected primary-expression before ';' token
testclass.cpp: In member function 'void TestClass<T>::setFunc(bool, 
bool, bool) [with T = int]':
testclass.cpp:109:   instantiated from here
testclass.cpp:57: error: address of overloaded function with no 
contextual type information
testclass.cpp:61: error: address of overloaded function with no 
contextual type information
testclass.cpp:68: error: address of overloaded function with no 
contextual type information
testclass.cpp:72: error: address of overloaded function with no 
contextual type information
testclass.cpp:82: error: address of overloaded function with no 
contextual type information
testclass.cpp:86: error: address of overloaded function with no 
contextual type information
testclass.cpp:93: error: address of overloaded function with no 
contextual type information
testclass.cpp:97: error: address of overloaded function with no 
contextual type information

GCC version is powerpc-apple-darwin8-gcc-4.0.1 (GCC) 4.0.1 (Apple 
Computer, Inc. build 5247)


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Template and member function pointers compile question
  2006-04-05 14:45 Template and member function pointers compile question Tristan Bonsall
@ 2006-04-05 14:55 ` John Love-Jensen
  2006-04-05 15:03   ` Tristan Bonsall
  0 siblings, 1 reply; 4+ messages in thread
From: John Love-Jensen @ 2006-04-05 14:55 UTC (permalink / raw)
  To: Tristan Bonsall, MSX to GCC

Hi Tristan,

Change this...

pFunc = &TestClass<T>::calculate<true,true,true>;

...to this...

pFunc = &TestClass<T>::template calculate<true,true,true>;

And do the same in the other similar areas.  Does that fix the issue?

--Eljay

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Template and member function pointers compile question
  2006-04-05 14:55 ` John Love-Jensen
@ 2006-04-05 15:03   ` Tristan Bonsall
  2006-04-05 15:25     ` Daniel Llorens del Río
  0 siblings, 1 reply; 4+ messages in thread
From: Tristan Bonsall @ 2006-04-05 15:03 UTC (permalink / raw)
  To: John Love-Jensen; +Cc: gcc-help

John Love-Jensen wrote:

>Hi Tristan,
>
>Change this...
>
>pFunc = &TestClass<T>::calculate<true,true,true>;
>
>...to this...
>
>pFunc = &TestClass<T>::template calculate<true,true,true>;
>
>And do the same in the other similar areas.  Does that fix the issue?
>
>--Eljay
>  
>
Thanks. That works for GCC and MSVC.

Could you point us to any documentation on this area, just out of interest?

Tristan


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Template and member function pointers compile question
  2006-04-05 15:03   ` Tristan Bonsall
@ 2006-04-05 15:25     ` Daniel Llorens del Río
  0 siblings, 0 replies; 4+ messages in thread
From: Daniel Llorens del Río @ 2006-04-05 15:25 UTC (permalink / raw)
  To: gcc-help


> Could you point us to any documentation on this area, just out of  
> interest?
>
> Tristan

Stroustrup 3rd ed. C.13.6.

A similar case in template_member.cpp in

	http://charm.cs.uiuc.edu/users/olawlor/ref/examples/cpp/

Daniel

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2006-04-05 15:25 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-04-05 14:45 Template and member function pointers compile question Tristan Bonsall
2006-04-05 14:55 ` John Love-Jensen
2006-04-05 15:03   ` Tristan Bonsall
2006-04-05 15:25     ` Daniel Llorens del Río

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).