[C++-sig] py++ with template member function of template class

Soloman soloman817 at msn.com
Tue Jun 17 20:09:01 CEST 2008


Hi,

I have met a problem using py++ (pyplusplus) to generate code.

First, let's see this code:
////////////////////////////////////////////////////////////////////////////////////
// template member function
class AFunc
{
public:
    template <typename T>
    T foo(const T& t) const { return t + 1; }
};

// we can only fully specialize function
template <>
string AFunc::foo<string>(const string& t) const { return "string"; };

// the following code is used to generate the instance of template functions
#ifdef PYTHONWRAP    
void AFuncInit()
{
    AFunc a;
    cout << a.foo<int>(1) << endl;
    cout << a.foo<float>(1.5f) << endl;
    cout << a.foo<string>("hello") << endl;
};
#endif

I followed the instruction in py++ homepage->FAQ, and this works, a call to a.foo<int> will generate an instance of that template member function.

But, then, I used template class, and it won't work any more:
/////////////////////////////////////////////////////////////////////////
// test template member function in template class
// THIS WON'T WORK!!!!!!!!!!!!!
template <typename A>
class BFunc
{
public:
    template <typename T>
    T foo(const T& t) const;
};

template <typename A> template <typename T>
T BFunc<A>::foo(const T& t) const { return t + 1; };

#ifdef PYTHONWRAP
typedef BFunc<int> BFunc_Int;
void BFuncInit()
{
    BFunc_Int a;
    //mem_fun(&BFunc_Int::foo<int>);
    //void * p = (void *)(&BFunc<int>::template foo<int>);
    cout << a.foo<int>(2) << endl;
    cout << a.foo<float>(2.5) << endl;
    //cout << a.foo<string>("hello") << endl;
};
#endif

Look into the generated code, I saw:
    bp::class_< AFunc >( "AFunc" )    
        .def( 
            "foo"
            , (::std::string ( ::AFunc::* )( ::std::string const & ) const)( &::AFunc::foo )
            , ( bp::arg("t") ) )    
        .def( 
            "foo"
            , (float ( ::AFunc::* )( float const & ) const)( &::AFunc::foo )
            , ( bp::arg("t") ) )    
        .def( 
            "foo"
            , (int ( ::AFunc::* )( int const & ) const)( &::AFunc::foo )
            , ( bp::arg("t") ) );

    bp::class_< BFunc< int > >( "BFunc_Int" );

obviously, a call to BFunc_Int::foo<int> hasn't create the template member function instance, can anybody help me?

Regards,
Xiang.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20080618/49ddb740/attachment.htm>


More information about the Cplusplus-sig mailing list