我有一个简单的主函数模板,我想要进行部分特化。
当我使用VS 2015更新3编译此代码时,第三个调用会出现错误(请参见注释)。为什么?我不明白为什么没有使用第三个特化版本。
谢谢, Fred
template< typename T >
void SetAttribute( const T& value )
{
static_assert( false, "SetAttribute: wrong type!" );
}
template<> void SetAttribute( const bool& value ) {}
template<> void SetAttribute( const std::wstring& value ) {}
template< typename T >
void SetAttribute( const typename std::enable_if< std::is_integral< T >::value >::type& value ) {}
int main()
{
SetAttribute( std::wstring( L"bla" ) );
SetAttribute( bool( true ) );
SetAttribute( std::uint32_t( 1 ) ); // error C2338: SetAttribute: wrong type!
return 0;
}
当我使用VS 2015更新3编译此代码时,第三个调用会出现错误(请参见注释)。为什么?我不明白为什么没有使用第三个特化版本。
谢谢, Fred
std::enable_if< std::is_integral< T >::value >::type
中,T 无法被推导。 - Jarod42invalid
和integrals
,并使用静态函数进行实现,然后在SetAttribute
中使用std::conditional
来选择您想要的结构体。http://en.cppreference.com/w/cpp/types/conditional - Raxvan