基于模板参数对模板实例化发出警告

Issue warning on template instantiation based on template arguments

如果我们的 headers 的用户使用某些模板化类型实例化模板,我们希望发出编译器警告,到目前为止我们通过模板专业化做到了这一点:

#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++7 完成了工作(当 deque_version 在代码中时发出警告,只要它被注释掉就不会发出警告)。但是,对于 g++-8 和 clang++ 5 到 8,警告总是发出,即使没有 select_container 被实例化(即从源中删除 foo 时)。 See on compiler-explorer.

使用别名的属性:

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 trunk。 Demo.