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()));
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;
My questions: What is the standard way to extract types from a template expression? What are the tradeoffs in both approaches?
source
share