请考虑以下代码。我的目的是使用不同的转换运算符(隐式),具体取决于struct B
的模板类型参数。
#include <type_traits>
#include <iostream>
using namespace std;
// just to simplify syntax
template <bool Tp_bool, typename Tp = void>
using Enable_if = typename std::enable_if<Tp_bool, Tp>::type;
template<typename T>
struct B {
T m_value;
explicit B(T val = T{}) :
m_value{val}
{ }
template<typename Q = T, Enable_if<sizeof(Q) < sizeof(int), int> = 0 >
operator size_t(){
cout << "narrow T\n";
return static_cast<size_t>(m_value);
}
template<typename Q = T, Enable_if<sizeof(Q) >= sizeof(int), int> = 0 >
operator Q(){
cout << "wide T\n";
return m_value;
}
};
int main() {
B<int> b{5};
char* pc = new char[b]; // !!!compile-time error here!!!
if(b == 5) /*no-op*/; // unrem the line above to see that SFINAE works fine
return 0;
}
这是我在上面的代码块中指示的行上出现的错误:
error: default type conversion can't deduce template argument for
'template<class Q, typename std::enable_if<(sizeof (Q) >= sizeof (int)), int>::type <anonymous> >
B<T>::operator Q() [with Q = Q;
typename std::enable_if<(sizeof (Q) >= sizeof (int)), int>::type <anonymous> = <enumerator>; T = int]'
造成这种情况的原因是什么,以及如何使其发挥作用?