模板专门化与g+一起工作+;但不能使用Visual C++; 我有一组模板代码,它在G++下编译得很好,但是现在当我尝试用Visual C++ 2010在Windows下构建时,我会遇到一系列错误。p> 我有一个模板函数集合,用于从Lua代码中获取和设置C++对象中的值。例如,我有以下模板: //类返回类型Getter函数 模板 国际卢奥岛(卢奥岛州*L) { T*obj=luaW_check(L,1);//从堆栈中获取用户数据,并检查其是否为T类型 luaU_push(L,(obj->*Getter)();//运行模板中指定的Getter函数,并推送 返回1; }
(可以找到完整的文件) 此处举例说明:模板专门化与g+一起工作+;但不能使用Visual C++; 我有一组模板代码,它在G++下编译得很好,但是现在当我尝试用Visual C++ 2010在Windows下构建时,我会遇到一系列错误。p> 我有一个模板函数集合,用于从Lua代码中获取和设置C++对象中的值。例如,我有以下模板: //类返回类型Getter函数 模板 国际卢奥岛(卢奥岛州*L) { T*obj=luaW_check(L,1);//从堆栈中获取用户数据,并检查其是否为T类型 luaU_push(L,(obj->*Getter)();//运行模板中指定的Getter函数,并推送 返回1; },c++,templates,visual-c++,C++,Templates,Visual C++,(可以找到完整的文件) 此处举例说明: static luaL\u reg TextArea\u MT[]= { //类返回类型Getter函数 {“GetCharacterSize”,luaU_get}, {NULL,NULL} }; 该getter的签名如下: unsigned int GetCharacterSize() const; 我遇到了一系列这样的错误: 2>C:\Users\Alex\Documents\Visual Studio 2010\Projects\game\
static luaL\u reg TextArea\u MT[]=
{
//类返回类型Getter函数
{“GetCharacterSize”,luaU_get},
{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
这是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';
}
实际上,这和给每个重载一个不同的名称没有什么区别…这是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';
}
实际上,这和给每个重载一个不同的名称没有什么不同…如果您可以强制用户选择函数的实际返回类型,那么下面的方法就行了。也许,它会对你有用:
#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 );
}
如果您可以强制用户选择函数的实际返回类型,则以下操作有效。也许,它会对你有用:
#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 );
}
TextArea::GetCharacterSize
的实际签名是什么?用GetCharacterSize
的签名更新了这个问题在我看来像是一个编译器错误——应该调用第139行的重载,但是调用第147行的重载。此外,术语挑剔——不能专门化函数模板,这只是超载,我以为是这样的。我只提到了专门化,因为错误消息确实如此。TextArea::GetCharacterSize
的实际签名是什么?用GetCharacterSize
的签名更新了这个问题在我看来像是一个编译器错误——应该调用第139行的重载,但是调用了第147行的重载。另外,术语吹毛求疵——不能专门化函数模板,这仅仅是重载。我想是这样的。我之所以提到专门化,是因为错误消息确实如此。如果这是一个已知的和可证明的错误,有人向MS提交过吗?@CodyGray,我没有,但我不知道在哪里提交这样的错误,或者如何检查它是否是一个已知的问题。@Alex:搜索现有的错误并在上发布新的错误。一旦您发现或提交了相关的bug,请在此处发布链接,以便我们对其进行投票。:-]如果它是一个已知的和可证明的错误,有人向MS提交过吗?@CodyGray,我没有,但我不知道在哪里提交这样的错误,或者如何检查它是否是一个已知的问题。@Alex:搜索现有的错误并在上发布新的错误。一旦您发现或提交了相关的bug,请在此处发布链接,以便我们对其进行投票。:-]