#include
#include
// Simplified version of the SFINAE problem
struct NotSet {};
struct TypeA {};
struct TypeB {};
// Forward declaration of template function
template
int get_default();
// Specialization exists only for TypeA
template
int get_default() {
return 42;
}
// Note: No specialization for TypeB
// SFINAE attempt to detect if specialization exists
template
struct has_get_default : std::false_type {};
template
struct has_get_default : std::true_type {};
// Template function that should only call get_default if specialization exists
template
void test_get_default() {
if constexpr (has_get_default::value) {
std::cout
Подробнее здесь: [url]https://stackoverflow.com/questions/79675417/how-can-i-make-sfinae-properly-detect-at-compile-time-whether-a-template-special[/url]
// Forward declaration of template function template int get_default();
// Specialization exists only for TypeA template int get_default() { return 42; } // Note: No specialization for TypeB
// SFINAE attempt to detect if specialization exists template struct has_get_default : std::false_type {}; template struct has_get_default : std::true_type {};
// Template function that should only call get_default if specialization exists template void test_get_default() { if constexpr (has_get_default::value) { std::cout