Does this SFINAE template have a name?

Acting under the rule "there is never anything new under the sun," I doubt that I am the first person to come up with this trick. I decided that I could stumble on something on the Internet, documenting it in the end, but I have not done it yet, so I decided that I would ask.

Its purpose is to selectively include certain functions, provided that they are relevant, without using a derived class.

Is there a name for this template? And does anyone have any useful information about this template or a template that works similarly?

template<typename T, size_t N> class Point { public: template<size_t P, typename T2=void> using Enable2D = typename std::enable_if<P == 2 && N == P, T2>::type; template<size_t P, typename T2=void> using Enable3D = typename std::enable_if<P == 3 && N == P, T2>::type; template<size_t P, typename T2=void> using Enable4D = typename std::enable_if<P == 4 && N == P, T2>::type; template<size_t P, typename T2=void> using Enable2DOrHigher = typename std::enable_if<P >= 2 && N == P, T2>::type; template<size_t P, typename T2=void> using Enable3DOrHigher = typename std::enable_if<P >= 3 && N == P, T2>::type; template<size_t P, typename T2=void> using Enable4DOrHigher = typename std::enable_if<P >= 4 && N == P, T2>::type; //Example use cases template<size_t P=N> static Enable2D<P, Point> withAngle(T angle, T magnitude = 1); template<size_t P=N> static Enable3D<P, Point> fromAngles(T psi, T theta, T magnitude = 1); template<size_t P=N> Enable2DOrHigher<P, T> const& x() const; template<size_t P=N> Enable2DOrHigher<P, T> const& y() const; template<size_t P=N> Enable2DOrHigher<P> setX(T const& t); template<size_t P=N> Enable2DOrHigher<P> setY(T const& t); template<size_t P=N> Enable3DOrHigher<P, T> const& z() const; template<size_t P=N> Enable3DOrHigher<P> setZ(T const& t); template<size_t P=N> Enable4DOrHigher<P, T> const& w() const; template<size_t P=N> Enable4DOrHigher<P> setW(T const& t); }; 
+6
source share
1 answer

I would not call it a model, but this is a well-known technique.

Basically, as a conditional interface , this method basically solves the problem of the mechanism for switching compilation time to turn class interfaces on and off. The general process provides tools for switching the existence of members (hence the term conditional compilation ).

The technique is more or less implemented as you propose (although the lack of alias templates was not a problem before C ++ 11), but the usual problem is a heavy, cluttered, confusing, and ugly template machine code circuit.

Solution to this problem A. Alexandrescu provided a presentation on this topic. Initially, a small mention of the needs for such a technique:

enter image description here

Bullet that says

  • This function only works with numbers.

refers to your methods and the need to have temporary compilation conditions to switch the existence of functions (conditional interface).

A proposal for a new language feature is being discussed. Since we all invented the wheel a couple of times, he says, why not a new language syntax that will allow us to do such things. His collaboration with H. Sutter , created a static if compile-time compiler that removes the need for workarounds like the one you mentioned. A simple use of this would be

 template<int D> struct Vector { double coordinates[D]; static if ( D ) { double x() { return coordinates[0]; } } static if ( D > 1 ) { double y() { return coordinates[1]; } } static if ( D > 2 ) { double z() { return coordinates[2]; } } }; 

Well, perhaps this is not the smartest use, but I think I'm communicating with this idea.

On the opposite side, now B. Stroustroup published a paper , where after realizing the problems, the static if addresses, he explains why this is an erroneous concept (pun intended) :), and its adoption would be a disaster for the language (oh!).

These were my two cents, judging by the level of participants in this “conversation”, I would like to receive some feedback on which side they are on, or if they are part of the standardization process, what will they vote for.

+4
source

All Articles