GCC7在同一类中推导出一个const修饰符

时间:2017-08-14 12:02:19

标签: c++ this const c++17 decltype

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

我想要的是一个结构,其行为类似于随机访问容器,并提供一些功能,如iterator begin(){...}const_iterator cbegin(){...},尤其是value_type operator[](size_type idx){...}等等。我想提供尽可能多的函数,无论实例是否为const。因此,实际上A a{}; a.begin();将返回Aa.cbegin()值引用的非常量引用。鉴于A const ac{}; ac.begin()ac.cbegin()应具有相同的常量引用类型。但是这些用例可能没有意义。 我可以限制使用仅与非const a结合非const迭代器可调用的情况(即允许a.begin(),但不允许a.cbegin())和ac仅使用const迭代器(即ac.cbegin(),但不是ac.begin())。这有道理吗?

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

#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;
}

1 个答案:

答案 0 :(得分:2)

  

我想在一个类中推断它是否使用const修饰符声明。

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

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

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