改善模板化类中的编译时错误消息

时间:2018-11-29 06:52:11

标签: c++ templates typetraits

我有一个模板化类,应该接受某种对象(在此示例中为字符串和双精度)的某种容器(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成员函数,它将为时过早。

2 个答案:

答案 0 :(得分:1)

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

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具有非类型参数的意义上说,数组仍然是邪恶的,因此您不能使用单个模板来检查容器,最终检查仍然是类型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>>;

这样,以后您不太可能获得流氓专精。

答案 1 :(得分:0)

您可以将支票移入班级(并拆分您的条件):

template <typename Container>
class Wrapper
{
    static_assert(checks::CorrectContainer<Container>::value,
                  "only std::vector/std::array allowed");
    static_assert(checks::CorrectType<typename Container::value_type>::value,
                  "only double and std::string");

public:
    // ...
};

Demo