1

我有一些在 Clang 上编译但被 Visual C++ 拒绝的代码。我设法将其分解为以下示例:

template <typename T>
static constexpr bool BoolValue = std::is_same_v<T, int>;

template <typename X>
struct Struct final
{
    template <typename T, typename U>
    static constexpr bool BoolValue = std::is_same_v<T, U>;

    template <typename Y>
    auto method() -> std::enable_if_t<BoolValue<X, Y>>;
};

template <typename X>
template <typename Y>
auto Struct<X>::method() -> std::enable_if_t<BoolValue<X, Y>> {}

似乎最后一行中的尾随返回类型应该是引用Struct::BoolValue而不是 ::BoolValue。它适用于 Clang,但被 Visual C++ 2019(最新版本)拒绝:

error C2977: 'BoolValue': too many template arguments
message : see declaration of 'BoolValue'
error C2993: 'unknown-type': is not a valid type for non-type template parameter '_Test'
error C2976: 'std::enable_if_t': too few template arguments
message : see declaration of 'std::enable_if_t'
error C2244: 'Struct<X>::method': unable to match function definition to an existing declaration
message : see declaration of 'Struct<X>::method'
message : definition
message : 'unknown-type Struct<X>::method(void)'
message : existing declarations
message : 'enable_if<Struct<X>::BoolValue<X,Y>,void>::type Struct<X>::method(void)'

我怀疑这是一个编译器错误,但我想先检查一下:这段代码有效吗?

4

0 回答 0