考虑以下代码(https://godbolt.org/z/s17aoczj6):
template<class T>
class Wrapper {
public:
explicit Wrapper(T t): _value(t) {}
template<class S = T>
operator T() { return _value; }
private:
T _value;
};
auto main() -> int
{
auto i = int{0};
auto x = Wrapper<int>(i);
return x + i;
}
Run Code Online (Sandbox Code Playgroud)
它使用 clang 进行编译,但不能使用 gcc(所有版本)进行编译。当删除template<class S = T>. 这段代码是否格式错误或者某个编译器错误?
gcc 中的错误是error: no match for 'operator+' (operand types are 'Wrapper<int>' and 'int') return x + i;.
我想要转换为T. 在此示例中,模板不是必需的,但在非最小示例中,我想使用 SFINAE,因此这里需要一个模板。