首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GCC7在同一类中推断常量修饰符的存在

GCC7在同一类中推断常量修饰符的存在
EN

Stack Overflow用户
提问于 2017-08-14 20:02:19
回答 1查看 54关注 0票数 0

我最初想在一个类中推断它是用常量修饰符声明的还是不用常量修饰符声明的。正如你们中的许多人所指出的,声明为const (而不是类)的是变量本身。谢谢你把这一点说清楚。错误消息现在对我来说完全有意义了。所以,这更多的是一个设计问题。

我想要的是一个结构,它的行为像一个随机访问容器,并提供一些功能,如iterator begin(){...}const_iterator cbegin(){...},特别是value_type operator[](size_type idx){...}等等。我希望提供尽可能多的函数,无论实例是否为const。因此,在实践中,A a{}; a.begin();将返回值类型为A的非常数引用,而a.cbegin()将返回常量引用。而对于A const ac{};ac.begin()ac.cbegin()应该具有相同的常量引用类型。但这些用例可能没有意义。我可以限制到只有结合非常数迭代器的非常数a是可调用的(例如,允许a.begin(),但不允许a.cbegin()),以及只允许使用常量迭代器的ac (即,ac.cbegin(),但不是ac.begin())。这有意义吗?

这种奇怪的尝试背后的原因是,在我的实现中,不存在一个底层容器,而是两个辅助容器:一个位向量和一个压缩序列。根据位向量的内容,我要么返回一个特殊符号,要么返回压缩序列的一个字母(参见扩展代码示例)。此外,我没有使用std::iterator,而是使用自己的实现。注意(*host)[idx + offset]返回一个临时的,这可能就是为什么我在输出后得到一个分段错误的原因。

代码语言:javascript
复制
#include <cassert>
#include <iostream>
#include <numeric>
#include <type_traits>
#include <vector>

template<typename container_t>
struct my_iterator
{
private:
    using reference = std::conditional_t<std::is_const<container_t>::value,
                    typename container_t::const_reference,
                    typename container_t::reference>;
    using size_type = typename container_t::size_type;
    size_type offset = 0;
    typename std::add_pointer_t<container_t> host{nullptr};
public:
    my_iterator(container_t & host_, size_type offset_) : host{&host_}, offset{offset_} {}
    reference operator[](typename container_t::size_type const idx)
    {
        return (*host)[idx + offset];
    }
};

template<typename sequence_t>
struct A  // implements some features of the container concept
{
    using const_reference = typename sequence_t::const_reference;
    using reference = typename sequence_t::value_type;
    using iterator = my_iterator<A>;
    using const_iterator = my_iterator<A const>;
    using value_type = typename sequence_t::value_type;
    using size_type = typename sequence_t::size_type;

    // data structures internally used to resolve random access
    std::vector<unsigned int> bit_vector{1,0,1,0,0,0};
    std::vector<char> text{'h', 'w'};
    constexpr char static const cash = '$';

public:
    // provide some container functions, like begin, end, cbegin, cend
    iterator begin()
    {
        return iterator{*this, 0};
    }

    const_iterator cbegin() const
    {
        return const_iterator{*this, 0};
    }
    // ...

    size_type rank(size_type idx) const
    {
        return std::accumulate(bit_vector.begin(),  bit_vector.begin()+idx, 0);
}

constexpr reference operator[](size_type const idx) const
{
    assert(idx < bit_vector.size());
    if (bit_vector[idx])
        return cash;
    return text[idx - rank(idx)];
}
};

int main(){
    /* non const usage */
    A<std::vector<char>> a{};
    auto it_a = a.begin();
    std::cout << it_a[0] << std::endl;
    /*  const usage */
    A<std::vector<char>> const a_const{};
    /* does not compile, because of non matching types */
    auto it_const_a = a_const.begin();
    std::cout << "it_const_a[0] = " << it_const_a[1] << std::endl;
    /* does compile, but gives segmentation fault */
    auto it_const_a2 = a_const.cbegin();
    std::cout << "it_const_a2[0] = " << it_const_a2[1] << std::endl;
}
EN

回答 1

Stack Overflow用户

发布于 2017-08-14 21:28:20

我想在一个类中推断,无论它是用常量修饰符声明的,还是不带常量修饰符的。,

类未使用const限定符声明。这些限定符用于变量的声明。

正如错误消息所解释的那样,在非静态成员函数之外没有this。类的成员类型(别名)不依赖于实例,因此不能依赖于实例的稳定性。

无论如何,我怀疑您假设std::iterator是一个迭代器。它不是一个迭代器。它是一个基类,可以用来在编写(自定义)迭代器时避免重复一些定义。这种混乱可能是为什么它将在即将到来的标准版本中被弃用的原因。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45674113

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档