c++ - 改进模板类中的编译时错误消息

标签 c++ templates typetraits

我有一个模板类,它应该接受某种对象(在此示例中为字符串和 double )的某种容器(std::array 和 std::vector)。

如果类是由错误的对象组合构造的,我的目标是提供一些明确的编译错误。 以下代码在 Visual Studio 17 版本 15.9.0 中编译。

///Performing checks 
namespace checks
{
    template <typename T>
    struct CorrectType {
        enum { value = false };
    };

    template <>
    struct CorrectType<std::string> {
        enum { value = true };
    };
    template <>
    struct CorrectType<double> {
        enum { value = true };
    };

    template <typename T>
    struct CorrectContainer {
        enum { value = false };
    };

    template <typename T, typename A>
    struct CorrectContainer<std::vector<T, A>> {
        enum { value = CorrectType<T>::value };
    };

    template <typename T, std::size_t N>
    struct CorrectContainer<std::array<T, N>> {
        enum { value = CorrectType<T>::value };
    };
    template <class Container>
    void constexpr check(){
        static_assert(checks::CorrectContainer<Container>::value, "Wrong container: only vectors/arrays of doubles/strings are accepted");
    }
}


template <typename Container>
class Wrapper
{
public:

    explicit Wrapper(const Container &container) :  container_(container), size_(container.size())
    {
  //type checking is performed
        checks::check<Container>();
    }

    void display() const {
        for (int i = 0; i < size_; i++)
            std::cout << this->container_[i] << " ";
        std::cout << std::endl;
    }

private:
    Container container_;
    int size_ = 0;
};

int main()
{
    //Ok
    Wrapper array_wrapper(std::array<double, 5>{0.0,1.0,2.0,3.0,4.0});
    array_wrapper.display();
    //Ok
    Wrapper string_wrapper(std::array<std::string, 3>{ "a","b","c" });
    string_wrapper.display();

    //Error - working as intended but not clear what went wrong
    Wrapper<std::vector<int>> vector_wrapper({ 1,2,3});
    vector_wrapper.display();
}

上面的代码按预期工作,但错误是模棱两可的:我们无法理解容器是否错误或所包含对象的种类是错误的。此外,如果模板化对象没有 size 成员函数,它会过早失败。

最佳答案

据我了解您的查询,无效类型检测可能概括为

template<class> struct ValidType;

template<template<class...> class> struct ValidContainer: std::false_type {};
template<> struct ValidContainer<std::vector>: std::true_type {};

template<class> struct ValidType: std::false_type {};
template<class> struct ValidType<double>: std::true_type {};
template<class> struct ValidType<std::string>: std::true_type {};

template<class> struct ValidArgument {
    static_assert(false, "Both container and element type are wrong");
};  

template<template<class...> class Ctr, class T, class... Ts>
struct ValidArgument<Ctr<T, Ts...>> {
    static_assert(ValidContainer<Ctr>::value || ValidType<T>::value
            , "Both container and element type are wrong");
    static_assert(ValidContainer<Ctr>::value, "Container type is wrong");
    static_assert(ValidType<T>::value, "Element type is wrong");
};

template<class T, std::size_t n> struct ValidArgument<std::array<T, n>> {
    static_assert(ValidType<T>::value, "Element type is wrong");
};  

(注意这不是真正的代码,只是一个想法的演示。)

数组仍然是邪恶的,因为 std::array 有一个非类型参数,因此你不能有一个检查容器的模板,最终检查仍然是kind 0 类型,在一般情况下进行容器检查,std::array 被单独处理。

或者,ValidType 可以稍微紧凑一些:

template<class T> using ValidType =             std::bool_constant<
        std::is_same_v<T, double> || std::is_same_v<T, std::string>>;

或者

template<class T> using ValidType = std::disjunction<
        std::is_same<T, double>, std::is_same<T, std::string>>;

或者一个不太标准的类型匹配类。例如:

template<class T, class... Ts> inline constexpr bool is_one_of_v =
        std::disjunction_v<std::is_same<T, Ts>...>;
template<class T> using ValidType =
        std::bool_constant<is_one_of_v<T, double, std::string>>;

这样你以后就不太可能获得流氓专精。

关于c++ - 改进模板类中的编译时错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53533368/

相关文章:

c++ - 使用函数指针将字符串数组作为参数传递

php - 是否可以在 Silverstripe 模板变量上运行一个函数来格式化输出?

c++ - 模板替换和 SFINAE 中的私有(private)成员访问

c++ - 模板类自动类型转换的运算符重载

c++ - std::is_convertible 是协变的还是逆变的?

c++ - 连续内存

c++ - 如何将 n 个参数传递给 n 未知的函数

C++ 模板 : how to determine if a type is suitable for subclassing

C++检测类型是否为字符串对象

c++ - 如何通过zeromq发送包含\0的序列化跳跃运动帧?