小编kcc*_*qzy的帖子

C++中的decltype(this)是什么类型的?

显然clang think decltype(this)是指向cv限定类的指针,而gcc认为它是指向cv限定类的指针的const引用.GCC只认为decltype(&*this)是指向cv限定类的指针.当它用作模板的类型名称时,这会产生一些影响.考虑一个假设的例子:

template<typename T>
class MyContainer {
    /* ... */
    template<typename ContainerPtr>
    class MyIterator {
        ContainerPtr container;
        /* ... */
    };
    auto cbegin() const
        -> MyIterator<decltype(&*this)> { return { /* ... */ }; }
    auto cend() const
        -> MyIterator<decltype(this)> { return { /* ... */ }; }
};
Run Code Online (Sandbox Code Playgroud)

在此示例中,实现了一个自定义容器T.作为容器,它支持迭代器.实际上,有两种迭代器:iterators和const_iterators.复制这两个代码没有意义,因此可以编写一个模板迭代器类,获取指向原始类MyContainer<T> *的指针或指向const版本的指针MyContainer<T> const *.

何时cbegincend一起使用时,gcc会出错,说它推断出冲突的类型,而clang只是工作正常.

c++ this decltype c++11

27
推荐指数
2
解决办法
6230
查看次数

标签 统计

c++ ×1

c++11 ×1

decltype ×1

this ×1