检测类型是否为枚举的向量

问题描述 投票:0回答:2

我收到一个错误:

错误:默认模板参数不能在部分专业化中使用

在以下代码中:

#include <iostream>
#include <type_traits>
#include <vector>

enum class MyEnum
{
    aaa,
    bbb,
};

template<class T>
struct is_vector_enum
{
    using type = T ;
    constexpr static bool value = false;
};

template<class T, class std::enable_if<std::is_enum<T>::value>::type* = nullptr>  // Error ....
struct is_vector_enum<std::vector<T>>
{
    using type = std::vector<T> ;
    constexpr static bool value = true;
};


int main()
{
    std::cout << "is_vector_enum: " << is_vector_enum<std::vector<MyEnum>>::value << std::endl;
}

目的是检测类型是否为枚举的向量。

我应该如何修复此代码?

c++ templates c++17 typetraits
2个回答
2
投票
您的主模板和您的专业需要具有相同数量的模板参数。目前,您的小学有1:

0
投票
专业化所允许的模板参数数量与主要参数不同。实际上,这种情况经常发生。但是,如错误所示,不允许您给其中任何一个默认参数。
© www.soinside.com 2019 - 2024. All rights reserved.