5

I often use SFINAE to remove a function from the overload set if the function body does not make sense(i.e. does not compile). Would it be possible to add to C++ a simple require statement?

For example, let's have a function:

template <typename T>
T twice(T t) {
  return 2 * t;
}

Then I get:

twice(1.0);
twice("hello");  // Error: invalid operands of types ‘int’ and ‘const char*’ to binary ‘operator*’

I want to get an error that says that there is not function twice for argument of type const char *

I would love to write something like:

template <typename T>
requires function_body_compiles
T twice(T t) {
  return 2 * t;
}

Then I would get

twice(1.0);
twice("hello");  // Error: no matching function for call to ‘twice2(const char [6])’

More motivation: I was watching the talk The Nightmare of Move Semantics for Trivial Classes and his final SFINAE is basically saying: use this constructor when it does compile. For a more complicated constructor writing the correct SFINAE would be a nightmare.

Do you think that adding requires function_body_compiles to c++ would make sense? Or is there a fundamental problem I'm missing? How badly could this be abused or misused?

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
tom
  • 1,520
  • 1
  • 12
  • 26
  • It would make SFINAE requirements for `T` unclear. One would have to examine the entire constructor body to determine if a particular `T` fits or not. – HolyBlackCat Nov 23 '18 at 06:50
  • 3
    Such a template could not be forward-declared. And it would permit misspellings or other simple typos to go undetected: Suppose the function body calls `printf` but you forgot to `#include `. Suddenly, the entire overload vanishes! And what would this be: `template requires function_body_compiles T once(T t) { return rand() ? once(t) : t; }`? The function body compiles if and only if the function body compiles. – Raymond Chen Nov 23 '18 at 07:07
  • @RaymondChen The forward-declaration is definitely a very valid point and yes the circular dependency is definitely a problem, but you can achieve this even now. Actually, Nicolai talks about it at that CppCon talk around 38:50. Although, I see your point with typos, but I think that it could be detected quite easily. You try to call a function, and the compiler will tell you that there is no such a function because the one you actually want to call was removed due to the typo. The only problem would be if there is another function overload match, does it happen often? – tom Nov 23 '18 at 07:42
  • Isn't this what concepts are all about? (and not contracts) – Matthieu Brucher Nov 23 '18 at 08:21
  • @MatthieuBrucher Maybe, I'm not sure. Is a mechanism for defining a concept by 'is this function compilable?' ? You can definitely define a concept by specifying a bunch of expressions which need to be valid for the type. – tom Nov 23 '18 at 09:22
  • I was wondering if instead of `requiring` a specific `type` how about `exclude` instead? It could be an idea for a new key word that could be used with templates so that the programmer can create a generic template for many types, but also exclude a specific set of types that are not to be used with that specific template. – Francis Cugler Nov 27 '18 at 04:55
  • 1
    @Francis Cugler Just require a negation of a predicate e.g. `require !std::is_same_v`. Of course, you can introduce a predicate or a concept with a better name but there is definitely no need for a new keyword. – tom Nov 27 '18 at 05:43
  • @tom Okay that makes sense, but the `require` and `concept` that should be available in C++ 20 is new to me; I have not read anything on it yet. – Francis Cugler Nov 27 '18 at 09:00

3 Answers3

7

The biggest reason why we don't have this feature is that it is hard.

It is hard, because it requires compilers be able to compile nearly arbitrary C++ code, get errors, then back out cleanly.

Existing C++ compilers where not all designed to do this. In fact, it took MSVC most of a decade to have reasonably compliant decltype SFINAE support.

Doing so for full function bodies would be even harder.


Now, even if it was easy, there are reasons not do do this. It mixes implementation and interface in a pretty horrible way.

Rather than go this way, the C++ committee is moving in a completely different direction.

Concepts are the idea that you can express requirements about types in sensible, usually named ways. They are coming in .

As another answer mentions,

template <typename T> requires requires(T t) { { 2 * t } -> T; }
T twice(T t) {
  return 2 * t;
}

is a way to do it, but that way is considered bad form. Instead, you should write a concept "can be multiplied by an integer and get the same type back".

template<typename T>
concept IntegerScalable = requires(T t) {
  { 2 * t } -> T;
};

we can then

template <IntegerScalable T>
T twice(T t) {
  return 2 * t;
}

and we are done.

A desired next step is called "checked concepts". In checked concepts, the concept it converted into a set of compile-time interfaces for your type T.

Then the body of the function is checked to ensure nothing is done to anything of type T that isn't a requirement of a concept.

Using a theoretical future checked concept,

template <IntegerScalable T>
T twice(T t) {
  T n = 7;
  if (n > t) return n;
  return 2 * t;
}

this would be rejected by the compiler when compiling the template even before a call to the template was done, because the concept IntegerScalable didn't guarantee that you could either initialize a T with an integer, nor that you could compare one T to another with >. Plus I think the above requires move-construction.


There is a hack you can do today.

#define RETURNS(...) \
  noexcept(noexcept(__VA_ARGS__)) \
  -> decltype(__VA_ARGS__) \
  { return __VA_ARGS__; }

then your code can be written as:

template<class T>
auto twice(T t)
RETURNS( 2 * t )

and you'll get a SFINAE friendly version of twice. It will also be as noexcept as it can be.

A variant of this using => to replace RETURNS and some other stuff was proposed by @Barry, but it has been a year since I've seen it move.

Meanwhile, RETURNS does most of the heavy lifting.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
2

There was a [proposal] filed by Barry Revzin for exactly what you are asking, but in context of lambda expressions. As it requires constructing lambda the syntax would be a bit different:

auto twice = [](auto t) => 2 * t; //sfinae friendly

or even:

auto twice = 2 * $0;

Nevertheless the status of this proposal is still uncertain. You can check it [here].

However in case of constructor I'm not sure if there would be a way to apply such a construct even if the proposal get accepted. Nevertheless if someone saw the need in case of lambda expressions there is probably potential for language development in the general case.

W.F.
  • 13,888
  • 2
  • 34
  • 81
  • This is nice, but for complicated functions you do not want to write a lambda. – tom Nov 23 '18 at 08:45
  • @tom exactly, now I don't see the possibility of what you want in the current standard wording, but when the committee finally accept the Barry's proposal I think the door would get wide open to apply some more interesting from your point of view language improvements... – W.F. Nov 23 '18 at 08:48
  • Hmm, when I think about it a bit more. This works only for a single line lambda, right? Then it is not as useful as I initially thought. If it is a single line you can as well write it in the trailing return type again and get the desired behavior. It is a bit verbose, but doable. – tom Nov 23 '18 at 09:53
  • @tom yup, but the expression still can be complex and extensive and duplicating it might be painful... This was the original cause behind the proposal I guess. When you think of it - your desirable behaviour would extend of the requirements to the level of entire block of code. – W.F. Nov 23 '18 at 10:27
  • @tom Once again I'd like to state that mentioning abbreviated lambda here is rather to point you to the direction of proposing new feature to the language as none of currently available nor the proposal I know of would suit your desired syntax. – W.F. Nov 23 '18 at 10:31
  • It's effectively dead. – T.C. Nov 26 '18 at 20:15
  • @T.C. does it mean committee think about rejecting it? – W.F. Nov 27 '18 at 11:45
2

You can do to some extent what you want with requires-expressions (https://godbolt.org/z/6FDT45):

template <typename T> requires requires(T t) { { 2 * t } -> T; }
T twice(T t) {
  return 2 * t;
}

int main()
{
twice(1.0);
twice("hello"); // Error: Constraints not satisfied
}

As you noted in the comments, a helper function cannot be used to avoid writting the function body twice, because errors in the implementation are not found until instantiation time. However, requires expressions benefit from advantages over decltype(expr) trailing return types:

  • They are not limited to return types.
  • There can be as many expressions as needed.

What you would like to have is referred to as "concept definition checking". Bjarne Stroustrup discusses why it is missing in the concepts design in the paper P0557R0 (Section 8.2).

metalfox
  • 6,301
  • 1
  • 21
  • 43
  • I do not need `require` for that, I can as easily do `auto twice(T t) -> decltype(2*t) {...}`. The problem is that you have to write the function body twice! This does not scale to more complicated functions. – tom Nov 23 '18 at 08:37
  • @tom Well. You can write a helper function, maybe in a detail namespace, and invoke it in a requires expression. – metalfox Nov 23 '18 at 08:41
  • I have no real experience with `require`, but would that really work? I know that this does not work for the `decltype` trick for sure. – tom Nov 23 '18 at 08:44
  • 1
    @tom. It does work. Here's a [link](https://godbolt.org/z/0QCC0V) for you to try. – metalfox Nov 23 '18 at 08:48
  • 1
    Nope it does not work. It does not remove the function from the overload set. To test that, I define the `twice` function without the `require`, if the `require` is not satisfied then the unconstrained version should be called. However, calling `twice("hello")` gives a compiler error instead of calling the unconstrained version. Have a look: https://godbolt.org/z/LvMXQi – tom Nov 23 '18 at 09:13