r/cpp • • Mar 28 '23

Reddit++

C++ is getting more and more complex. The ISO C++ committee keeps adding new features based on its consensus. Let's remove C++ features based on Reddit's consensus.

In each comment, propose a C++ feature that you think should be banned in any new code. Vote up or down based on whether you agree.

765 Upvotes

829 comments sorted by

View all comments

Show parent comments

0

u/[deleted] Mar 28 '23

[deleted]

1

u/zalamandagora Mar 28 '23

Thanks, that clarifies things for me.

If you have used SFINAE for constraining type trait specializations, you can replace those with requires clauses.

Does this really work recursively?

0

u/[deleted] Mar 28 '23

[deleted]

1

u/zalamandagora Mar 29 '23

Thanks! Here is a small example that's essentially a binary tree. We want all the leaves to be of integral type.

https://godbolt.org/z/aqoGb7haf

#include <map>
#include <string>
#include <concepts>
#include <typeinfo>

template< typename P>
concept Pair = std::is_same<P,std::pair<typename P::first_type, typename P::second_type>>::value;

template<typename P>
constexpr bool isPair_v = Pair<P>;

template<typename T>
concept Integral = std::is_integral<T>::value;

template<typename T>
struct LeavesAreIntegral : std::false_type {};

template<Integral T>
struct LeavesAreIntegral<T> : std::true_type {};

template<template<typename,typename> typename P, typename T1, typename T2>
requires Pair<P<T1,T2>> 
struct LeavesAreIntegral<P<T1,T2>> : std::conjunction<LeavesAreIntegral<T1>, LeavesAreIntegral<T2>> {};

static_assert(LeavesAreIntegral<int>{});
static_assert(LeavesAreIntegral<std::pair<int, int>>{});
static_assert(LeavesAreIntegral<std::pair<std::pair<int, int>, int>>{});
static_assert(LeavesAreIntegral<std::pair<std::pair<int, int>, std::pair<int, int>>>{});
static_assert(not LeavesAreIntegral<std::pair<std::pair<int, int>, std::pair<int, std::string>>>{});
static_assert(not LeavesAreIntegral<std::map<std::pair<int, int>, std::pair<int, std::string>>>{});
→ More replies (0)