Skip to main content
The 2024 Developer Survey results are live! See the results

Questions tagged [sfinae]

Substitution failure is not an error. This is a C++ programming technique that allows templates to verify properties about their template parameters, thus allowing different specializations to be used if certain kinds of objects are available.

1 vote
1 answer
45 views

Why do a SFINAE function template and a regular function template have different binding rules?

I had a checker function that used the requires keyword to detect if a target function was defined or not. I wanted to make it work with C++17, so I switched the checker from using requires to using ...
Joshua Maiche's user avatar
-1 votes
0 answers
44 views

How to write C++ template SFINAE in my case [duplicate]

template<typename VertexType> class CQuad { public: VertexType tl, bl, tr, br; void SetColor(const CColor& color) { //if VertexType has a member "color", then ...
Louis's user avatar
  • 1
-10 votes
1 answer
86 views

SFINAE-kindish function definition BUT without templates [closed]

Example Many consumers of the following class class A{ double a; void dump(){ std::ofstream ooo("dump.txt"); ooo << A.a << "\n"; ooo.close(); } }; will ...
user23311233's user avatar
1 vote
1 answer
63 views

C++ function dispatch based on user defined input type category (C++11/14 friendly)

I want to dispatch functions based on user defined "tag" of my input types. For example, I defined a tag called SomeSpecificObjectType and in class Object I mark this class as a ...
Xiyang Liu's user avatar
4 votes
2 answers
97 views

C++ ambiguous member vs non-member function template resolution in GCC 14 but not in prior GCC versions

The following code compiles fine with GCC 13 and earlier, but GCC 14.1 produces an "ambiguous overload" error. Is it a compiler or code problem, and, more pragmatically, can I make the ...
akryukov's user avatar
1 vote
1 answer
106 views

Template function accepting a pointer-to-member refuses to compile [duplicate]

Consider this class: template <typename T> struct Buffer { template <typename Field> int add_member_attribute( std::string&& name, Field T::* ...
trbabb's user avatar
  • 2,005
3 votes
1 answer
119 views

How can `if constexpr` be made SFINAE-friendly?

I have a code using "classic" SFINAE. template<class M> auto power(M const& elem) -> decltype(std::norm(elem)) { return std::norm(elem); } template<class M, class = std::...
alfC's user avatar
  • 15.6k
1 vote
0 answers
85 views

A question in writing is_container and output STL data

My code is #include <type_traits> #include <iostream> template<typename T, typename = void> struct is_container : std::false_type {}; template<typename T> struct is_container ...
softdream's user avatar
-2 votes
2 answers
127 views

How to check whether a function template exists in C++?

Is there a way to check the existence of a function (of a specific signature) regardless of whether it is templated or not? For example: template <bool FLAG> class A { public: template <...
zhanginou's user avatar
  • 185
0 votes
1 answer
56 views

Specialize function template for all types of pointers

I want to implement a streaming style logging lib. I made a Log_t class to buffer a log entry, and do real output up on it being destroyed, like this: class Log_t: public std::ostringstream { ...
Leon's user avatar
  • 1,997
1 vote
2 answers
85 views

How to invoke template function with maybe incomplete type argument

Template function func<T> is designed to be used any where. It has the same function body for any T. I want to write its body only once. But the function depends on the definition type<T> ...
王雨泽's user avatar
0 votes
0 answers
74 views

How does SFINAE affects symbol?

Given helpers as follow : template <typename T, typename = void> struct is_complete : std::false_type {}; template <typename T> struct is_complete<T, decltype(void(sizeof(T)))> : ...
王雨泽's user avatar
1 vote
1 answer
93 views

Function template correct definition for forward reference to an array and type of element

Minimal working example: #include <array> #include <iostream> template <typename T> struct is_array : std::false_type {}; template <typename U, std::size_t N> struct is_array&...
deponovo's user avatar
  • 1,391
2 votes
1 answer
96 views

Alias for class template parameter in C++

I have following code which detects if given type T is std::hashable. It checks validity of the passed parameters as well as if result of std::hash<T>::operator() is convertible to size_t: ...
Marcin's user avatar
  • 35
2 votes
3 answers
83 views

c++ metaprogramming: 'nullptr' vs '0' as a default paramater for "enable_if<bool>::type* = [x]"

I'm new to metaprogramming, and I've encountered a certain behavior I couldn't understand. It seems that you can use '0' as a default-function-parameter, but not as a default-template-parameter. and I ...
Ben Edri's user avatar

15 30 50 per page
1
2 3 4 5
123