compilation error with function template parameter

Discussion in 'C++' started by subramanian100in@yahoo.com, India, Aug 27, 2010.

  1. , India

    , India Guest

    The following quesion is NOT a HOMEWORK problem.

    Consider the following x.cpp:

    #include <cstdlib>
    #include <iostream>

    using namespace std;

    template<class T>
    inline void fn(void (T::*memfnPtr)())
    {
    return;
    }

    class Test
    {
    public:
    void member();
    void member() const;
    };

    inline void Test::member(void)
    {
    cout << "Test::member() called" << endl;

    return;
    }

    inline void Test::member(void) const
    {
    cout << "Test::member() const called" << endl;

    return;
    }

    int main()
    {
    fn(&Test::member);

    return EXIT_SUCCESS;
    }

    When I compile this program with g++3.4.3 as
    g++ -std=c++98 -pedantic -Wall -Wextra x.cpp

    I get the following compilation error:
    x.cpp: In function `int main()':
    x.cpp:35: error: no matching function for call to `fn(<unknown type>)'

    Why do I get this compilation error ? Kindly explan. Please help to
    fix this error. I do not get the compilation error, if I have the
    function template 'fn()' as follows:
    template<class T>
    inline void fn(void (T::*memfnPtr)() const)
    {
    return;
    }

    Note that in the above modified version of 'fn()', I have added
    'const' to the parameter. Why don't I get the compilation error
    for this version but I do get for the earlier version ?

    Thanks
    V.Subramanian
    , India, Aug 27, 2010
    #1
    1. Advertising

  2. On 8/27/2010 8:09 AM, , India wrote:
    > The following quesion is NOT a HOMEWORK problem.
    >
    > Consider the following x.cpp:
    >
    > #include<cstdlib>
    > #include<iostream>
    >
    > using namespace std;
    >
    > template<class T>
    > inline void fn(void (T::*memfnPtr)())
    > {
    > return;
    > }
    >
    > class Test
    > {
    > public:
    > void member();
    > void member() const;
    > };
    >
    > inline void Test::member(void)
    > {
    > cout<< "Test::member() called"<< endl;
    >
    > return;
    > }
    >
    > inline void Test::member(void) const
    > {
    > cout<< "Test::member() const called"<< endl;
    >
    > return;
    > }
    >
    > int main()
    > {
    > fn(&Test::member);
    >
    > return EXIT_SUCCESS;
    > }
    >
    > When I compile this program with g++3.4.3 as
    > g++ -std=c++98 -pedantic -Wall -Wextra x.cpp
    >
    > I get the following compilation error:
    > x.cpp: In function `int main()':
    > x.cpp:35: error: no matching function for call to `fn(<unknown type>)'
    >
    > Why do I get this compilation error ? Kindly explan. Please help to
    > fix this error. I do not get the compilation error, if I have the
    > function template 'fn()' as follows:
    > template<class T>
    > inline void fn(void (T::*memfnPtr)() const)
    > {
    > return;
    > }
    >
    > Note that in the above modified version of 'fn()', I have added
    > 'const' to the parameter. Why don't I get the compilation error
    > for this version but I do get for the earlier version ?


    Most likely a bug in the compiler. Comeau C++ accepts the code without
    a hiccup. So does Visual C++ 2008 (it points out that the argument of
    the template 'fn' is unused, but it's a warning).

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Aug 27, 2010
    #2
    1. Advertising

  3. , India

    mingze zhang Guest

    On Aug 27, 8:24 pm, Victor Bazarov <> wrote:
    > On 8/27/2010 8:09 AM, , India wrote:
    >
    >
    >
    >
    >
    > > The following quesion is NOT a HOMEWORK problem.

    >
    > > Consider the following x.cpp:

    >
    > > #include<cstdlib>
    > > #include<iostream>

    >
    > > using namespace std;

    >
    > > template<class T>
    > > inline void fn(void (T::*memfnPtr)())
    > > {
    > >          return;
    > > }

    >
    > > class Test
    > > {
    > > public:
    > >      void member();
    > >      void member() const;
    > > };

    >
    > > inline void Test::member(void)
    > > {
    > >          cout<<  "Test::member() called"<<  endl;

    >
    > >          return;
    > > }

    >
    > > inline void Test::member(void) const
    > > {
    > >          cout<<  "Test::member() const called"<<  endl;

    >
    > >          return;
    > > }

    >
    > > int main()
    > > {
    > >          fn(&Test::member);

    >
    > >          return EXIT_SUCCESS;
    > > }

    >
    > > When I compile this program with g++3.4.3 as
    > > g++ -std=c++98 -pedantic -Wall -Wextra x.cpp

    >
    > > I get the following compilation error:
    > > x.cpp: In function `int main()':
    > > x.cpp:35: error: no matching function for call to `fn(<unknown type>)'

    >
    > > Why do I get this compilation error ? Kindly explan. Please help to
    > > fix this error. I do not get the compilation error, if I have the
    > > function template 'fn()' as follows:
    > > template<class T>
    > > inline void fn(void (T::*memfnPtr)() const)
    > > {
    > >          return;
    > > }

    >
    > > Note that in the above modified version of 'fn()', I have added
    > > 'const' to the parameter. Why don't I get the compilation error
    > > for this version but I do get for the earlier version ?

    >
    > Most likely a bug in the compiler.  Comeau C++ accepts the code without
    > a hiccup.  So does Visual C++ 2008 (it points out that the argument of
    > the template 'fn' is unused, but it's a warning).
    >
    > V
    > --
    > I do not respond to top-posted replies, please don't ask



    Additional info: On g++ 4.4.3-4ubuntu5, the above code is fine too.

    3.4.3 was released in November 4, 2004, probably it's too old to
    differentiate the pointers to member functions overloaded by const?
    mingze zhang, Aug 30, 2010
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. , India
    Replies:
    13
    Views:
    580
    Alf P. Steinbach
    Aug 16, 2007
  2. Stuart Redmann
    Replies:
    5
    Views:
    467
    Stuart Redmann
    Dec 14, 2007
  3. aaragon
    Replies:
    6
    Views:
    1,173
    aaragon
    Mar 23, 2008
  4. Lionel B
    Replies:
    2
    Views:
    484
    Lionel B
    Jul 25, 2008
  5. AzamSharp
    Replies:
    2
    Views:
    159
Loading...

Share This Page