模板专业化适用于g ++,但不适用于Visual C ++

时间:2011-12-30 22:16:11

标签: c++ templates visual-c++

我有一堆模板化代码可以在g ++下编译,但是现在当我尝试使用Visual C ++ 2010在Windows下构建时,我遇到了一堆错误。

我有一组模板函数,用于从Lua代码中获取和设置C ++对象中的值。例如,我有这个模板:

//        Class       Return type Getter function
template <typename T, typename U, U (T::*Getter)() const>
int luaU_get(lua_State* L)
{
    T* obj = luaW_check<T>(L, 1); // Gets userdata from stack and checks if it's of type T
    luaU_push(L, (obj->*Getter)()); // Runs the getter function specified in the template, and pushes the
    return 1;
}

(可以找到完整的文件here

这是在这里实例化的:

static luaL_reg TextArea_MT[] =
{
    //                             Class     Return type   Getter function
    { "GetCharacterSize", luaU_get<TextArea, unsigned int, &TextArea::GetCharacterSize> },
    { NULL, NULL }
};

该getter的签名如下:

unsigned int GetCharacterSize() const;

我遇到了很多这样的错误:

2>C:\Users\Alex\Documents\Visual Studio 2010\Projects\game\dev\src\game\lua\LuaTextArea.cpp(103): error C2440: 'specialization' : cannot convert from 'unsigned int (__thiscall ag::ui::TextArea::* )(void) const' to 'unsigned int *(__thiscall ag::ui::TextArea::* const )(void) const'
2>          Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
2>C:\Users\Alex\Documents\Visual Studio 2010\Projects\game\dev\src\game\lua\LuaTextArea.cpp(103): error C2973: 'luaU_get' : invalid template argument 'unsigned int (__thiscall ag::ui::TextArea::* )(void) const'
2>          C:\Users\Alex\Documents\Visual Studio 2010\Projects\game\dev\src\extern\LuaWrapper\LuaWrapperUtil.hpp(147) : see declaration of 'luaU_get'
2>C:\Users\Alex\Documents\Visual Studio 2010\Projects\game\dev\src\game\lua\LuaTextArea.cpp(103): error C2440: 'specialization' : cannot convert from 'unsigned int (__thiscall ag::ui::TextArea::* )(void) const' to 'unsigned int *ag::ui::TextArea::* const '
2>          There is no context in which this conversion is possible
2>C:\Users\Alex\Documents\Visual Studio 2010\Projects\game\dev\src\game\lua\LuaTextArea.cpp(103): error C2973: 'luaU_get' : invalid template argument 'unsigned int (__thiscall ag::ui::TextArea::* )(void) const'
2>          C:\Users\Alex\Documents\Visual Studio 2010\Projects\game\dev\src\extern\LuaWrapper\LuaWrapperUtil.hpp(131) : see declaration of 'luaU_get'
2>C:\Users\Alex\Documents\Visual Studio 2010\Projects\game\dev\src\game\lua\LuaTextArea.cpp(103): error C2440: 'specialization' : cannot convert from 'unsigned int (__thiscall ag::ui::TextArea::* )(void) const' to 'unsigned int ag::ui::TextArea::* const '
2>          There is no context in which this conversion is possible
2>C:\Users\Alex\Documents\Visual Studio 2010\Projects\game\dev\src\game\lua\LuaTextArea.cpp(103): error C2973: 'luaU_get' : invalid template argument 'unsigned int (__thiscall ag::ui::TextArea::* )(void) const'
2>          C:\Users\Alex\Documents\Visual Studio 2010\Projects\game\dev\src\extern\LuaWrapper\LuaWrapperUtil.hpp(123) : see declaration of 'luaU_get'
2>C:\Users\Alex\Documents\Visual Studio 2010\Projects\game\dev\src\game\lua\LuaTextArea.cpp(103): error C2440: 'initializing' : cannot convert from 'overloaded-function' to 'lua_CFunction'
2>          None of the functions with this name in scope match the target type

2 个答案:

答案 0 :(得分:4)

这是VC ++中的编译器错误。以下代码有效:

#include <iostream>

struct TextArea
{
    unsigned GetCharacterSize() const { return 0; }
};

template<typename T, typename U, U (T::*)() const>
int foo()
{
    return 1;
}

template<typename T, typename U, U* (T::*)() const>
int foo()
{
    return 2;
}

int main()
{
    std::cout << foo<TextArea, unsigned, &TextArea::GetCharacterSize>() << '\n';
}

使用GCC 4.3.4GCC 4.5.1和Comeau 4.3.10.1 Beta2(无链接)进行编译,但VC ++ 2010 SP1会产生以下错误:

  

错误C2668:'foo':对重载函数的模糊调用


编辑:至于解决方法,它很丑陋,但我唯一可以想到的是使用额外的间接层,以便不会涉及过载:

#include <iostream>

struct WithPointer
{
    unsigned* GetCharacterSize() const { return nullptr; }
};

struct WithoutPointer
{
    unsigned GetCharacterSize() const { return 0u; }
};

template<bool UsePointerImplB>
struct kludge
{
    template<typename T, typename U, U (T::*Getter)() const>
    static int foo() { return 1; }
};

template<>
struct kludge<true>
{
    template<typename T, typename U, U* (T::*Getter)() const>
    static int foo() { return 2; }
};

int main()
{
    std::cout
        << kludge<false>::foo<WithoutPointer, unsigned, &WithoutPointer::GetCharacterSize>() << '\n'
        << kludge<true>::foo<WithPointer, unsigned, &WithPointer::GetCharacterSize>() << '\n';
}

实际上,这与为每个重载赋予不同的名称没有什么不同......

答案 1 :(得分:1)

如果您可以强制用户选择该函数的实际返回类型,则以下方法有效。也许,它对你有用:

#include <iostream>

struct FooBar
{
  int Foo( void ) const
  {
   std::cout << "FooBar::Foo()" << std::endl;
   return ( 0 );
  }
  int * Bar( void ) const
  {
   std::cout << "FooBar::Bar()" << std::endl;
   return ( 0 );
  }
};

template< typename P00, typename P01, P01(P00::*p02)( void ) const >
void Call()
{
 P00 lT;
 ( lT.*p02 )();
}

int main( void )
{
 Call< FooBar, int, &FooBar::Foo > ();
 Call< FooBar, int*, &FooBar::Bar > ();

 return( 0 );
}

节目输出:

FooBar::Foo()
FooBar::Bar()