public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Tristan Bonsall <tristan_bonsall@carallon.com>
To: gcc-help@gcc.gnu.org
Subject: Template and member function pointers compile question
Date: Wed, 05 Apr 2006 14:45:00 -0000	[thread overview]
Message-ID: <4433D819.4060202@carallon.com> (raw)

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)


             reply	other threads:[~2006-04-05 14:45 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-04-05 14:45 Tristan Bonsall [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4433D819.4060202@carallon.com \
    --to=tristan_bonsall@carallon.com \
    --cc=gcc-help@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).