sfinae

How does using ellipses for SFINAE work?

空扰寡人 提交于 2021-02-20 08:59:25
问题 When using SFINAE to select constructor overloads in the past, I have typically used the following: template <typename T> class Class { public: template <typename U = T, typename std::enable_if<std::is_void<U>::value, int>::type=0> Class() { std::cout << "void" << std::endl; } template <typename U = T, typename std::enable_if<!std::is_void<U>::value, int>::type=0> Class() { std::cout << "not void" << std::endl; } }; However, I just came across this alternative: template <typename U = T,

c++ log functions using template SFINAE for conditional compile

前提是你 提交于 2021-02-19 08:34:44
问题 I am evaluating if it is possible to leverage C++11 features to replace logging Macros without any run-time additional cost. I come out with this demo: enum class LogLevel { Fatal = 0, DFatal = 1, Error = 2, Normal = 3, Verbose = 4, Debug = 5 }; constexpr LogLevel log_compiled = LogLevel::Normal; LogLevel log_runtime = LogLevel::Error; #ifdef NDEBUG constexpr LogLevel log_fatal = LogLevel::Fatal; #else constexpr LogLevel log_fatal = LogLevel::DFatal; #endif template <LogLevel L, typename std:

if constexpr vs sfinae

。_饼干妹妹 提交于 2021-02-19 00:55:05
问题 With the introduction of if constexpr in c++17 , some problems which were solved by using compile-time SFINAE in c++14 / c++11 can now be solved using if constexpr , with an easier syntax. Consider, e.g., the following basic example of a compile-time recursion to produce a subroutine which prints a variable number of arguments. #include <iostream> #include <type_traits> template <typename T> void print_sfinae(T&& x) { std::cout << x << std::endl; } template <typename T0, typename... T> std:

How to detect if a class has member variables?

吃可爱长大的小学妹 提交于 2021-02-18 10:57:25
问题 Problem I would like to detect if a class has member variables and fail a static assert if they do. Something like: struct b { int a; } static_assert(!has_member_variables<b>, "Class should not contain members"). // Error. struct c { virtual void a() {} void other() {} } static_assert(!has_member_variables<c>, "Class should not contain members"). // Fine. struct d : c { } static_assert(!has_member_variables<d>, "Class should not contain members"). // Fine. struct e : b { } static_assert(!has

How to check if type is explicitly/implicitly constructible?

喜夏-厌秋 提交于 2021-02-18 06:25:10
问题 How can it be checked that some type is explicitly (or vice versa implicitly) constructible from other type? Is it any SFINAE trick in this situation? I can write is_explicitly_constructible as a combination of std::is_constructible and std::is_convertible: #include <type_traits> template <typename Type, typename Argument> struct is_explicitly_constructible : std::bool_constant < std::is_constructible<Type, Argument>::value && !std::is_convertible<Argument, Type>::value > { }; But do I take

Partial template function specialization with enable_if: make default implementation

こ雲淡風輕ζ 提交于 2021-02-17 18:54:10
问题 Using C++11's enable_if I want to define several specialized implementations for a function (based on the type of the parameter, say) as well as a default implementation. What is the correct way to define it? The following example does not work as intended since the "generic" implementation is called, whatever the type T . #include <iostream> template<typename T, typename Enable = void> void dummy(T t) { std::cout << "Generic: " << t << std::endl; } template<typename T, typename std::enable

How to detect a noexcept method using SFINAE

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-10 05:29:25
问题 I'm asking about a variation of a (popular) question - detecting the existence of a method of a class. I've read many answers here in SO, and most of the (post C++17) solutions look like this: #include <type_traits> template<class ...Ts> struct voider{ using type = void; }; template<class T, class = void> struct has_foo : std::false_type{}; template<class T> struct has_foo<T, typename voider<decltype(std::declval<T>().foo())>::type> : std::true_type{}; Basically, we're letting the compiler

Code using SFINAE working with GCC but not with Clang

拈花ヽ惹草 提交于 2021-02-10 05:12:52
问题 I'm trying to use SFINAE in C++11 to implement a serialization library. My code works fine with GCC but not with Clang. I've reduced it here to a minimal code: template <typename A, typename T> constexpr auto has_save_method(A& ar, T& t) -> decltype(t.save(ar), bool()) { return true; } template<class A, typename T, bool has_save> struct saver; template<class A, typename T> struct saver<A,T,true> { static void apply(A& ar, T& t) { t.save(ar); } }; class MyClass { public: template<typename A>

Using SFINAE to disable template class member function

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-08 13:22:09
问题 Is it possible to use SFINAE and std::enable_if to disable a single member function of a template class? I currently have a code similar to this: #include <type_traits> #include <iostream> #include <cassert> #include <string> class Base { public: virtual int f() { return 0; } }; template<typename T> class Derived : public Base { private: T getValue_() { return T(); } public: int f() override { assert((std::is_same<T, int>::value)); T val = getValue_(); //return val; --> not possible if T not

Private member access in template substitution and SFINAE

僤鯓⒐⒋嵵緔 提交于 2021-02-08 12:21:37
问题 class A { int a; }; template<typename, typename = void> class test {}; template<typename T> class test<T,decltype(T::a)> {}; int main() { test<A> a; } The code above compiles without error on clang version 3.8.0-2ubuntu4 (tags/RELEASE_380/final) , but fails to compile on g++-5 (Ubuntu 5.4.1-2ubuntu1~16.04) 5.4.1 20160904 and g++-6 (Ubuntu 6.2.0-3ubuntu11~16.04) 6.2.0 20160901 with errors like this: main.cpp: In function ‘int main()’: main.cpp:9:22: error: ‘int A::a’ is private within this