根据模板参数发出有关模板实例化的警告

时间:2019-01-28 14:36:24

标签: c++ templates g++ clang++ gcc-warning

如果标头的用户使用某些模板化类型实例化模板,我们要发出编译器警告,到目前为止,我们通过模板专门化来做到这一点:

ALTER USER root@localhost IDENTIFIED WITH mysql_native_password BY 'mot-passe';

这可以在g ++ 7上完成工作(在代码中#include <deque> #include <vector> template <typename T, template <typename...> class CONTAINER> struct select_container { using _t = CONTAINER<T>; // we don't have custom allocators for most containers }; template <typename T> struct select_container<T, std::vector> { using _t = std::vector<T>; // meant for custom allocator }; template <typename T> struct select_container<T, std::deque> { using _t = std::deque<T>; // custom allocator should also go here [[deprecated("We won't stop you from using deque, but please think twice " "(link to wiki).")]] constexpr static inline int __bad() { return 0; } enum { _bad = __bad() }; }; int foo() { select_container<int, std::vector>::_t vector_version; // select_container<int, std::deque>::_t deque_version; return vector_version[0]; } 时发出警告,并且只要被注释掉就不发出警告)。但是,对于g ++-8和clang ++ 5至8,即使没有实例化deque_version(即从源中删除select_container),也会始终发出警告。 See on compiler-explorer

1 个答案:

答案 0 :(得分:3)

使用别名上的属性:

template <typename T>
struct  select_container<T, std::deque> {
  using _t [[deprecated("We won't stop you from using deque, but please think twice "
               "(link to wiki).")]] = std::deque<T>;  // custom allocator should also go here
};

这适用于gcc和clang干线。 Demo