Let's say I have a class template A that has one type template parameter, and a single primary specialization:
template<typename T> struct A {
/*...*/
};
For some T
arguments A<T>
will instantiate successfully, for others it won't.
Without modifying or extending the definition of A
, is it possible to write a bool variable template:
template<typename T>
constexpr bool WorksWithA = /*...*/;
such that WorkWithA<T>
is true iff A<T>
would instantiate successfully?
Update
Posted this as separate question: Using typename in C++20 requires / concept?
#include <iostream>
template<typename T>
struct A {
using X = typename T::X;
};
template<typename T>
constexpr bool WorksWithA = requires { typename A<T>; };
struct GoodArg {
using X = int;
};
struct BadArg {
};
int main() {
std::cout << WorksWithA<GoodArg> << std::endl;
std::cout << WorksWithA<BadArg> << std::endl;
}
Compiled and run with:
$ clang++ --version
clang version 10.0.0-4ubuntu1
$ clang++ test.cc -std=c++20
$ ./a.out
1
1
This outputs 1 1
, expected output is 1 0
?
What gives?
std::is_constructible
– NathanOliverA
is not SFINAE friendly, I don't think you can have yourWorksWithA
automatically. – Jarod42WorksWithA
(and any associated machinery) to continue to give the right answer without modification. – Andrew TomazosA
yields a type, object construction constructs an object – 463035818_is_not_a_number