C ++ template metaprogramming: how to derive a type in an expression template

I need a static check on the type of the lambdas parameter. I wrote this code below and it seems to give the correct result.

struct B { };
auto lamBc = [](B const& b) { std::cout << "lambda B const" << std::endl; };

template<typename ClosureType, typename R, typename Arg>
constexpr auto ArgType(R (ClosureType::*)(Arg) const)->Arg;

template <typename T>
using ArgType_t = decltype(ArgType(&T::operator()));

// ArgType_t<lamBc> is "reference to B const"

However, I noticed that, for example, the standard library uses the class template specialization to extract the reference type from the reference type in std::remove_reference. So I tried this approach, and it also seems to give the correct result.

template<typename L>
struct ArgType2;

template<typename ClosureType, typename R, typename Arg>
struct ArgType2<R (ClosureType::*)(Arg) const>
{
    typedef Arg type;
};

template <typename T>
using ArgType2_t = typename ArgType2<decltype(&T::operator())>::type;

// ArgType2_t<lamBc> is also "reference to B const"

My questions: What is the standard way to extract types from a template expression? What are the tradeoffs in both approaches?

+4
source share
2 answers

Both of your approaches are valid, interchangeable, and produce the same result (infer the parameter type lambda accepts).

(. 23.15.1 ), :

  • UnaryTypeTrait . , , , , ....

  • BinaryTypeTrait . , , , , ....

  • A TransformationTrait . , , , , ....

, , decltype , ( boost, , ., , this).

, , , decltype.

, ++ 11 , .

+1

"", decltype() ++, PTS ++.

0

Source: https://habr.com/ru/post/1691762/


All Articles