构造函数重载和 SFINAE
constructor overloading and SFINAE
作为理解 std::enable_if
用法的练习,我尝试实现一个包装器 class(结构)来表示任何给定时间点的特定类型:
#include<type_traits>
#include<typeinfo>
#include<iostream>
using std::enable_if;
using std::is_same;
using std::cout;
using std::endl;
template<typename T>
struct type_wrap{
type_wrap(typename enable_if<is_same<int,T>::value,T>::type&& rrT):value(rrT){
cout << "The wrapped type is " << typeid(value).name() << endl;
cout << "The wrapped value is " << value << endl;
}
type_wrap(typename enable_if<is_same<float,T>::value,T>::type && rrT):value(rrT){
cout << "The wrapped type is " << typeid(value).name() << endl;
cout << "The wrapped value is " << value << endl;
}
T& value;
};
int main(){
type_wrap<int>(0);
type_wrap<float>(0.5);
return(0);
}
以上代码无法编译:
so_main.cpp:16:47: error: no type named 'type' in 'std::__1::enable_if<false, int>'; 'enable_if' cannot be used to disable this declaration
type_wrap(typename enable_if<is_same<float,T>::value,T>::type && rrT):value(rrT){
^~~~~~~~~~~~~~~~~~~~~~~
so_main.cpp:26:9: note: in instantiation of template class 'type_wrap<int>' requested here
type_wrap<int>(0);
^
so_main.cpp:12:47: error: no type named 'type' in 'std::__1::enable_if<false, float>'; 'enable_if' cannot be used to disable this declaration
type_wrap(typename enable_if<is_same<int,T>::value,T>::type&& rrT):value(rrT){
^~~~~~~~~~~~~~~~~~~~~
so_main.cpp:27:9: note: in instantiation of template class 'type_wrap<float>' requested here
type_wrap<float>(0.5);
^
2 errors generated.
如果我要删除其中一个重载的构造函数,并从 main()
中删除相应的实例,该代码就可以工作。但这违背了本练习的全部目的。
谁能指出编译错误的原因?
Can someone point out the cause for the compilation error?
因为 std::enable_if
将使您的构造函数之一非法,具体取决于以下各项:
type_wrap<int>(0);
type_wrap<float>(0.5);
int
或 double
会强制 std::is_same
的另一边有 false
, in which case std::enable_if
has no type
:
template<bool B, class T = void>
struct enable_if {}; // int or float will get this on each constructor.
template<class T>
struct enable_if<true, T> { typedef T type; };
相反,使用模板特化如下:
template<typename T>
struct type_wrap;
template<>
struct type_wrap<float>
{
type_wrap(float&& rrT) :value(rrT) {
cout << "The wrapped type is " << typeid(value).name() << endl;
cout << "The wrapped value is " << value << endl;
}
float& value;
};
template<>
struct type_wrap<int>
{
type_wrap(int&& rrT) :value(rrT) {
cout << "The wrapped type is " << typeid(value).name() << endl;
cout << "The wrapped value is " << value << endl;
}
int& value;
};
如果你的编译器支持 C++17,if constexpr
会让这更容易和更多 straight-forward:
template<typename T>
struct type_wrap
{
type_wrap(T&& rrT):value(rrT)
{
if constexpr (std::is_same<int, T>::value)
{
cout << "The wrapped type is " << typeid(value).name() << endl;
cout << "The wrapped value is " << value << endl;
}
else
{
cout << "The wrapped type is " << typeid(value).name() << endl;
cout << "The wrapped value is " << value << endl;
}
}
T& value;
};
SFINAE 在模板方法(/构造函数)上工作,这是您的 class 模板,您可以使用以下内容(即使在您的情况下似乎是 simpler/better):
template<typename T>
struct type_wrap{
template <typename U,
std::enable_if_t<std::is_same<int, U>::value
&& is_same<int, T>::value>* = nullptr>
type_wrap(U arg) : value(arg){
// Int case
std::cout << "The wrapped type is " << typeid(value).name() << std::endl;
std::cout << "The wrapped value is " << value << std::endl;
}
template <typename U,
std::enable_if_t<std::is_same<float, U>::value
&& is_same<float, T>::value>* = nullptr>
type_wrap(U arg) : value(arg){
// float case
std::cout << "The wrapped type is " << typeid(value).name() << std::endl;
std::cout << "The wrapped value is " << value << std::endl;
}
T value;
};
作为理解 std::enable_if
用法的练习,我尝试实现一个包装器 class(结构)来表示任何给定时间点的特定类型:
#include<type_traits>
#include<typeinfo>
#include<iostream>
using std::enable_if;
using std::is_same;
using std::cout;
using std::endl;
template<typename T>
struct type_wrap{
type_wrap(typename enable_if<is_same<int,T>::value,T>::type&& rrT):value(rrT){
cout << "The wrapped type is " << typeid(value).name() << endl;
cout << "The wrapped value is " << value << endl;
}
type_wrap(typename enable_if<is_same<float,T>::value,T>::type && rrT):value(rrT){
cout << "The wrapped type is " << typeid(value).name() << endl;
cout << "The wrapped value is " << value << endl;
}
T& value;
};
int main(){
type_wrap<int>(0);
type_wrap<float>(0.5);
return(0);
}
以上代码无法编译:
so_main.cpp:16:47: error: no type named 'type' in 'std::__1::enable_if<false, int>'; 'enable_if' cannot be used to disable this declaration
type_wrap(typename enable_if<is_same<float,T>::value,T>::type && rrT):value(rrT){
^~~~~~~~~~~~~~~~~~~~~~~
so_main.cpp:26:9: note: in instantiation of template class 'type_wrap<int>' requested here
type_wrap<int>(0);
^
so_main.cpp:12:47: error: no type named 'type' in 'std::__1::enable_if<false, float>'; 'enable_if' cannot be used to disable this declaration
type_wrap(typename enable_if<is_same<int,T>::value,T>::type&& rrT):value(rrT){
^~~~~~~~~~~~~~~~~~~~~
so_main.cpp:27:9: note: in instantiation of template class 'type_wrap<float>' requested here
type_wrap<float>(0.5);
^
2 errors generated.
如果我要删除其中一个重载的构造函数,并从 main()
中删除相应的实例,该代码就可以工作。但这违背了本练习的全部目的。
谁能指出编译错误的原因?
Can someone point out the cause for the compilation error?
因为 std::enable_if
将使您的构造函数之一非法,具体取决于以下各项:
type_wrap<int>(0);
type_wrap<float>(0.5);
int
或 double
会强制 std::is_same
的另一边有 false
, in which case std::enable_if
has no type
:
template<bool B, class T = void>
struct enable_if {}; // int or float will get this on each constructor.
template<class T>
struct enable_if<true, T> { typedef T type; };
相反,使用模板特化如下:
template<typename T>
struct type_wrap;
template<>
struct type_wrap<float>
{
type_wrap(float&& rrT) :value(rrT) {
cout << "The wrapped type is " << typeid(value).name() << endl;
cout << "The wrapped value is " << value << endl;
}
float& value;
};
template<>
struct type_wrap<int>
{
type_wrap(int&& rrT) :value(rrT) {
cout << "The wrapped type is " << typeid(value).name() << endl;
cout << "The wrapped value is " << value << endl;
}
int& value;
};
如果你的编译器支持 C++17,if constexpr
会让这更容易和更多 straight-forward:
template<typename T>
struct type_wrap
{
type_wrap(T&& rrT):value(rrT)
{
if constexpr (std::is_same<int, T>::value)
{
cout << "The wrapped type is " << typeid(value).name() << endl;
cout << "The wrapped value is " << value << endl;
}
else
{
cout << "The wrapped type is " << typeid(value).name() << endl;
cout << "The wrapped value is " << value << endl;
}
}
T& value;
};
SFINAE 在模板方法(/构造函数)上工作,这是您的 class 模板,您可以使用以下内容(即使在您的情况下似乎是 simpler/better):
template<typename T>
struct type_wrap{
template <typename U,
std::enable_if_t<std::is_same<int, U>::value
&& is_same<int, T>::value>* = nullptr>
type_wrap(U arg) : value(arg){
// Int case
std::cout << "The wrapped type is " << typeid(value).name() << std::endl;
std::cout << "The wrapped value is " << value << std::endl;
}
template <typename U,
std::enable_if_t<std::is_same<float, U>::value
&& is_same<float, T>::value>* = nullptr>
type_wrap(U arg) : value(arg){
// float case
std::cout << "The wrapped type is " << typeid(value).name() << std::endl;
std::cout << "The wrapped value is " << value << std::endl;
}
T value;
};