While experimenting with some template constraint constructs, I encountered a surprising behavior in 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;
This decltype triggers a SFINAE context in my compiler:
template<constraint_success* value>
using constraint = decltype(value);
As opposed to:
//template<constraint_success* value>
//using constraint = constraint_success*;
Example:
//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;
}
Is this required by the standard, or is this a "lucky" compiler bug?