问题可能很奇怪,所以这里是一个简短的激励示例:
#include <vector>
#include <type_traits>
template <typename T>
// workaround for gcc 8.3 where volatile int is not trivially copyable
using is_tc = std::is_trivially_copyable<std::remove_cv<T>>;
// static assert passes compile, oops
static_assert(is_tc<std::vector<int>>::value);
如您所见,错误是我已将类型特征本身传递给另一个类型特征,而不是传递::type
或使用std::remove_cv_t
。
最明显的解决方案是,我不要犯错误,但是我想知道C ++类型特征是否可以限制它们的输入类型,以便它们不接受其他type_traits作为参数。现在的难题是,type_traits中有大量类型特征,因此IDK如何最好地实现这一点。
[注意:我并不是说C ++应该这样做,我知道要防止罕见的bug要做很多工作,我只是想了解更复杂的概念设计,而您的约束并不基于类型的语义(aka具有++和*),但实际上类型属于大量类型(并且该类型包含您要限制的类型)。
问题可能很奇怪,所以这里是一个简短的激励示例:#include
::type
作为参数,这是一种快速的解决方法:template<class T> concept HasType = requires { typename T::type; };
template<class T> concept HasNoType = !HasType<T>;
template<HasNoType T> using remove_cv = std::remove_cv<T>;
template<HasNoType T> using remove_cv_t = typename remove_cv<T>::type;