在尝试一些模板约束构造时,我在Clang 3.7中遇到了令人惊讶的行为:
struct constraint_success {};
struct constraint_failure {};
template<bool>
struct require_t {};
template<>
struct require_t<true> {
static constexpr constraint_success* result = nullptr;
};
template<>
struct require_t<false> {
static constexpr constraint_failure* result = nullptr;
};
template<bool Condition>
constexpr auto require = require_t<Condition>::result;
//A named dummy value, since we need a default
constexpr constraint_success* required = nullptr;
此decltype在我的编译器中触发SFINAE上下文:
template<constraint_success* value>
using constraint = decltype(value);
相反:
//template<constraint_success* value>
//using constraint = constraint_success*;
示例:
//define custom constraints
template<typename T>
constexpr auto Pointer = require<std::is_pointer<T>::value>;
template<typename T>
constexpr auto NotPointer = require<!std::is_pointer<T>::value>;
//constrain template parameters
template<typename T, constraint<Pointer<T>> = required>
void foo() {
std::cout << "Hello, pointer!\n";
}
template<typename T, constraint<NotPointer<T>> = required>
void foo() {
std::cout << "Hello, not pointer!\n";
}
int main() {
foo<int*>();
foo<int>();
return 0;
}
这是标准所要求的,还是这是一个幸运的&#34;编译器错误?
答案 0 :(得分:8)
constraint
之类的别名模板会在他们使用的任何模板定义中被替换。它们以这种方式特殊:一旦提供了明确的参数,其他模板就会被替换。
所以,这个宣言:
template<typename T, constraint<Pointer<T>> = required>
void foo();
大致相当于此声明(差异是将static_cast
替换为隐式转换):
template<typename T, decltype(static_cast<constraint_success*>
(require_t<std::is_pointer<T>>::result))
= required>
void foo();
当static_cast
无效时,这会生成SFINAE。
效果很有用,本质上允许您模拟即将发布的Concepts功能中的约束。但是,这种特殊方法相当复杂,而且你并没有真正利用它。
规范的SFINAE方法是:
template< typename T >
std::enable_if_t< std::is_pointer< T >::value > foo();
你可以更干净,更聪明,并从返回类型中移除SFINAE - 您目前的目标:
template< typename T,
std::enable_if_t< std::is_pointer< T >::value > * = nullptr >
void foo();
鉴于Library Fundamentals TS中的变量模板,这与您的示例对应符号符号:
template< typename T,
std::enable_if_t< std::is_pointer_v< T > > * = nullptr >
void foo();
// constraint < Pointer <T>> = required>
真正的Concept-ish方式是这样的:
template< typename T, std::enable_if_t< std::is_pointer< T >::value > * = nullptr >
using Pointer = T;
template< typename T >
void foo( Pointer<T> );