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.

Substitution failure is not an error (SFINAE) refers to a situation in C++ where an invalid substitution of template parameters during template argument deduction is not in itself an error.

It is thus possible to select from multiple specializations of template functions based on the type of the parameter, because the substitution during template argument deduction does not cause an error in the compilation. The failure in the substitution removes the candidate from the overload list and hence is not considered.

David Vandevoorde first introduced the acronym SFINAE to describe related programming techniques.

1799 questions
0
votes
1 answer

"undeclared identifier" with MSVC++ 17.34 (VS 22) within decltype-based SFINAE expression

We are trying to update a C++17 project from VS 19 to VS 22, and all of a sudden, our code does not compile anymore. The problem in question arises in two headers of the foonathan/memory libraries, pretty far from our code. The library's own tests…
burnpanck
  • 1,955
  • 1
  • 12
  • 36
0
votes
1 answer

How to store and process possibly duplicate distributed class coupling information in C++?

I'm writing a template class SortedTypeHolder. During compile time instantiation SortedTypeHolder must collect distributed information about all provided ...Ts from their headers for future sorting. The gathered information describes…
soad
  • 49
  • 6
0
votes
1 answer

Why do partial and full C++ template specializations, that look almost the same, produce different results?

I haven't written many C++ templates till recently, but I'm trying to take a deep dive into them. Fixing one developer's code I managed to produce some compiler behavior that I can't explain. Here is the simplified program. ( When I try to simplify…
0
votes
1 answer

SFINAE in C++98

I am trying to understand how does SFINAE work in C++98. My aim is to write a simple template function that would only be called when an integer is passed. But the plan is to do it without specializing a function for typename T = int, but definining…
carce-bo
  • 480
  • 2
  • 10
0
votes
1 answer

SFINAE for detecting if size method is implemented doesn't work with vector of vector

I expect this code will work with vector of vector but it seems to detect the existence of size only for the external vector. Anyone can help? #include #include // SFINAE test template class has_size { …
Fzza
  • 105
  • 1
  • 9
0
votes
1 answer

How to implement metaprogramming in c++ without std::enable_if

I was reading somewhere that it would be possible to do c++ metaprogramming using function overloads instead of SFINAE. I came up with a toy exercise to test this. I want to detect if a particular type is a nested vector. My code that works…
bradgonesurfing
  • 30,949
  • 17
  • 114
  • 217
0
votes
1 answer

What is the advantage of Hana's type_c-and-declval dance when querying whether a SFINAE-friendly expression is valid?

On the one hand, the function boost::hana::is_valid is presented as follows Checks whether a SFINAE-friendly expression is valid. Given a SFINAE-friendly function, is_valid returns whether the function call is valid with the given arguments.…
Enlico
  • 23,259
  • 6
  • 48
  • 102
0
votes
1 answer

accessing class member regardless of it being a function or a data member

I want to write a generic accessor for a class member regardless whether it is a function or or a data member: #include namespace traits { template struct _name; template
Sergey Kolesnik
  • 3,009
  • 1
  • 8
  • 28
0
votes
1 answer

Passing template parameter pack to type_traits and std::enable_if

I am trying to create a class holding std::variant with a member function that would only accept types held by the nested variant object. That function works basically the same way as variant's operator=. However, the question is - how do I use…
J_S
  • 2,985
  • 3
  • 15
  • 38
0
votes
2 answers

Disable to_json and from_json if member variable types don't support nlohmann's json library

I'm using nlohmann's single header json library to serialise a class I wrote. I want to use this class with various types (including but not limited to boost's multiprecision types). The problem is that some types including boost's…
0
votes
1 answer

C++: Why can't I invoke a template member function of a template parameter type?

I am writing a template function where one of the template parameters is a type with a member function that is itself a template function. When I invoke the template member function and explicitly specify the template parameters, it appears that the…
wvn
  • 624
  • 5
  • 12
0
votes
1 answer

SFINAE unresolved external symbol when putting definition in src file

I have the following preprocessor class, for which I would like to have separate implementations for Real or complex data. PreProcessor.cuh #ifndef __PREPROCESSOR_TEST_CUH__ #define __PREPROCESSOR_TEST_CUH__ #include #include…
rinkert
  • 6,593
  • 2
  • 12
  • 31
0
votes
1 answer

Using SFINAE to check if member exists in class based on a template

The example here shows how we can use TMP to have the compiler elide functions based on whether a member exists for a given type. I want to write a metafunction that works for classes based on templates. Compilation should succeed if a class based…
Arthur M
  • 447
  • 5
  • 21
0
votes
1 answer

Templated requires clause fails

I have a set of classes roughly defined as follows: template class Iterable { // More code }; class Container : public Iterable { // More code }; class Tuple : public Container { // More code }; class List { …
Felix Crazzolara
  • 982
  • 9
  • 24
0
votes
1 answer

SFINAE when using lvalue ref but success when using rvalue ref

I searched but really couldn't find an answer why SFINAE happens only when the argument is passed by lvalue ref, but the build succeeds when the arg is passed by rvalue ref: template class A { public: using member_type =…
llxxee
  • 59
  • 1
  • 6
1 2 3
99
100