如何在检测成语中要求精确的函数签名?

时间:2017-07-22 02:15:43

标签: c++ c++11 templates types type-conversion

假设我有一个类型T,我想检测它是否有一个下标运算符,我可以用另一种类型Index调用它。以下示例运行正常:

#include <type_traits>
#include <vector>

template < typename T, typename Index >
using subscript_t = decltype(std::declval<T>()[std::declval<Index>()]);

int main()
{
    using a = subscript_t< std::vector<int>, size_t >;
    using b = subscript_t< std::vector<int>, int    >;
}

然而,当且仅当函数签名完全匹配时,我希望检测函数。在上面的示例中,我希望语句subscript_t< std::vector<int>, int >;抛出类似no viable overloaded operator[]的错误,因为std::vector的下标运算符的签名是

std::vector<T, std::allocator<T>>::operator[](size_type pos);

GCC中的size_typeunsigned long。如何避免从intsize_t的隐式转换?

2 个答案:

答案 0 :(得分:5)

使用is_detected,您可以:

template <typename T, typename Ret, typename Index>
using subscript_t = std::integral_constant<Ret (T::*) (Index), & T::operator[]>;


template <typename T, typename Ret, typename Index>
using has_subscript = is_detected<subscript_t, T, Ret, Index>;

static_assert(has_subscript<std::vector<int>, int&, std::size_t>::value, "!");
static_assert(!has_subscript<std::vector<int>, int&, int>::value, "!");

Demo

我在SO文档中写道:

is_detected

概括type_trait创建:基于SFINAE  有实验特征detected_ordetected_tis_detected

使用模板参数typename Defaulttemplate <typename...> Optypename ... Args

  • is_detectedstd::true_typestd::false_type的别名,具体取决于Op<Args...>的有效性
  • detected_tOp<Args...>nonesuch的别名取决于Op<Args...>的有效性。
  • detected_orvalue_tis_detected的结构的别名,typeOp<Args...>Default,具体取决于有效性Op<Args...>

可以使用std::void_t为SFINAE实施,如下所示:

namespace detail {
    template <class Default, class AlwaysVoid,
              template<class...> class Op, class... Args>
    struct detector
    {
        using value_t = std::false_type;
        using type = Default;
    };

    template <class Default, template<class...> class Op, class... Args>
    struct detector<Default, std::void_t<Op<Args...>>, Op, Args...>
    {
        using value_t = std::true_type;
        using type = Op<Args...>;
    };

} // namespace detail

// special type to indicate detection failure
struct nonesuch {
    nonesuch() = delete;
    ~nonesuch() = delete;
    nonesuch(nonesuch const&) = delete;
    void operator=(nonesuch const&) = delete;
};

template <template<class...> class Op, class... Args>
using is_detected =
    typename detail::detector<nonesuch, void, Op, Args...>::value_t;

template <template<class...> class Op, class... Args>
using detected_t = typename detail::detector<nonesuch, void, Op, Args...>::type;

template <class Default, template<class...> class Op, class... Args>
using detected_or = detail::detector<Default, void, Op, Args...>;

然后可以简单地实现检测方法存在的特征:

template <typename T, typename ...Ts>
using foo_type = decltype(std::declval<T>().foo(std::declval<Ts>()...));

struct C1 {};

struct C2 {
    int foo(char) const;
};

template <typename T>
using has_foo_char = is_detected<foo_type, T, char>;

static_assert(!has_foo_char<C1>::value, "Unexpected");
static_assert(has_foo_char<C2>::value, "Unexpected");

static_assert(std::is_same<int, detected_t<foo_type, C2, char>>::value,
              "Unexpected");

static_assert(std::is_same<void, // Default
                           detected_or<void, foo_type, C1, char>>::value,
              "Unexpected");
static_assert(std::is_same<int, detected_or<void, foo_type, C2, char>>::value,
              "Unexpected");

答案 1 :(得分:1)

你可以这样做:

template <typename Index, typename ClassType, typename ReturnType>
constexpr bool arg_t(ReturnType (ClassType::*)(Index))
{
    return true;
}

template <typename T, typename Index>
struct subscript_t
{
    static_assert(arg_t<Index>(&T::operator[]));
};

请注意,您需要实例化subscript_t,而不仅仅是为其类型命名:

int main()
{
    subscript_t< std::vector<int>, size_t > a;
    subscript_t< std::vector<int>, int    > b;
}

另一种方法,可让您确定确切的错误消息:

template <typename ClassType, typename ReturnType, typename ArgType>
constexpr ArgType arg_t(ReturnType (ClassType::*)(ArgType))
{
    return {};
}

template <typename T, typename Index>
struct subscript_t
{
    using Actual = decltype(arg_t(&T::operator[]));
    static_assert(std::is_same<Index, Actual>::value, "oops");
};