如何检查类是否具有运算符[]?

问题描述 投票:2回答:2

我正在尝试检查operator []在某些类中是否过载。有我的代码:

template <class T, class = void>
struct has_operator : std::false_type {};

template <class T>
struct has_operator < T,
            std::void_t<decltype(std::declval<T>[](int i))>> :
            std::true_type {};

但是实际上不起作用。

如果班级为operator (),我将尝试编写类似代码的巫婆che声>

这里是:

template <typename T,class=void>
struct callable_without_args: std::false_type{};

template <typename T>
struct callable_without_args<T
          ,std::void_t<decltype(std::declval<T>()())>>
  :std::true_type{};

[我正在尝试检查运算符[]在某些类中是否过载。有我的代码:template struct has_operator:std :: false_type {};模板结构...

c++11 c++17 metaprogramming template-meta-programming typetraits
2个回答
0
投票

没有完全清楚您到底想要什么,但是...以下decltype()是错误的


0
投票
// ........................VVVVVVVVVVVVVVVVV   U value
decltype(std::declval<T>()[std::declval<U>()])
© www.soinside.com 2019 - 2024. All rights reserved.