【发布时间】:2017-01-19 16:32:08
【问题描述】:
如果我给了
typedef std::vector<int> v;
那么下面可以用来捕获常量迭代器的类型(另一种方法是使用v::const_iterator,但这取决于在类中显式定义的const_iterator成员类型。
typedef typename std::result_of<decltype(&v::cbegin)(v*)>::type const_iterator;
确实,我们可以检查上面的内容是否如我们所愿。
static_assert(std::is_same<const_iterator, typename v::const_iterator>::value);
但是,我在下面发现编译器失败。
typedef typename std::result_of<decltype(&v::begin)(v*)>::type iterator;
编译器抱怨该方法被重载(通过 const 修饰符)并且无法明确解决。但是,我找不到解决歧义的语法。至少,人们希望下面的内容是明确的,因为只有 const 版本才能对 const 对象进行操作。然而,即使是下面的也同样有问题。
typedef typename std::result_of<decltype(&v::begin)(const v*)>::type const_iterator2;
如何引用特定的 const 或 nonconst 版本的 begin?
【问题讨论】:
标签: c++ static constants typedef decltype