Skip to content

Latest commit

 

History

History
129 lines (95 loc) · 2.54 KB

298.md

File metadata and controls

129 lines (95 loc) · 2.54 KB
Info

Example

struct s {
  static constexpr auto operator()() { return 1; }
};
auto l = [] static { return 2; };

static_assert(3 == s{}() + l());

static_assert(3 == s::operator()() +
         decltype(l)::operator()());

https://godbolt.org/z/43hhKcr59

Puzzle

  • Can you implement count which returns number of types which can be called with static operator()?
template<class... Ts>
constexpr auto count = 0; // TODO

struct e {};
struct s {
    static constexpr auto operator()() -> void;
};
struct ns {
    constexpr auto operator()() -> void;
};

auto l = [] { };
auto sl = [] static {};

static_assert(0 == count<e>);
static_assert(1 == count<s>);
static_assert(0 == count<ns>);
static_assert(0 == count<decltype(l)>);
static_assert(1 == count<decltype(sl)>);
static_assert(2 == count<e, s, ns, decltype(l), decltype(sl)>);

https://godbolt.org/z/McrMfasjq

Solutions

template<class... Ts>
constexpr auto count = (requires { (void*)&Ts::operator(); } + ... + 0);

https://godbolt.org/z/Td6zxxaoM

template<class... Ts>
constexpr auto count = (0 + ... + requires{Ts::operator();});

https://godbolt.org/z/P74br35qh

template <class... Ts>
constexpr auto count = (... + requires { Ts::operator()(); });

https://godbolt.org/z/KoP4sxrxr

template <class T>
concept is_static_call_operator = requires{T::operator()();};

template<class... Ts>
constexpr auto count = (0 + ... + is_static_call_operator<Ts>)

https://godbolt.org/z/TEPKG3xa8

template<class... Ts>
constexpr auto count =
(0 + ... + requires{Ts::operator()();})

https://godbolt.org/z/f1xKoEvKz

template<class T>
constexpr auto has_static_operator = [](){
    if constexpr (requires { T::operator();}){
        return 1;
    }
    else{
        return 0;
    }
}();

template<class... Ts>
constexpr auto count = [](){
    return 0 + (... + has_static_operator<Ts>);
}();

https://godbolt.org/z/j3a6o78Wf

template<typename T>
concept HasStaticOp = requires(T) { T::operator(); };

template<class... Ts>
constexpr auto count = (HasStaticOp<Ts> + ...);

https://godbolt.org/z/Yz53ebYn9

template<class... Ts>
constexpr auto count = [] -> int {
    return (requires {Ts::operator()();}+...);
}();

https://godbolt.org/z/ne73vMMPW