相关疑难解决方法(0)

如何删除类似const和非const成员函数之间的代码重复?

假设class X我想要返回内部成员的访问权限:

class Z
{
    // details
};

class X
{
    std::vector<Z> vecZ;

public:
    Z& Z(size_t index)
    {
        // massive amounts of code for validating index

        Z& ret = vecZ[index];

        // even more code for determining that the Z instance
        // at index is *exactly* the right sort of Z (a process
        // which involves calculating leap years in which
        // religious holidays fall on Tuesdays for
        // the next thousand years or so)

        return ret;
    }
    const …
Run Code Online (Sandbox Code Playgroud)

c++ const class code-duplication c++-faq

228
推荐指数
11
解决办法
3万
查看次数

如果从不通过它们进行修改,是否允许 const 强制转换对实际 const 对象的引用的常量性?

我有一个声明常量和非常量成员函数的抽象类。为了讨论起见,让我们说它看起来像这样:

class record_interface
{
public:
   virtual ~record_interface() = default;

   virtual void set_foo(BoundedFloat) = 0;
   virtual BoundedFloat get_foo() const = 0;
};
Run Code Online (Sandbox Code Playgroud)

这用作记录的高级表示,当保存到光盘并通过线路传输时,该记录具有不同的表示。所以大多数实现只需要将它们的成员转换为所需的高级表示。

作为有效实现的示例,让我们定义stored_record. 这用于以有损格式存储高级记录:

struct stored_record
{
    int16_t foo;
};
Run Code Online (Sandbox Code Playgroud)

stored_record可以实现是有道理的,record_interface但由于各种原因它不能(例如,它需要是trivially_copyable)。我们可以制作一个为其实现接口的包装器:

class record_wrapper : public record_interface
{
public:
  record_wrapper(stored_record & wrapped)
    : wrapped_(wrapped) {}

  void set_foo(BoundedFloat value) final { wrapped_.foo = convert_to_int16(value); }
  BoundedFloat get_foo() const final { return convert_from_int16(wrapped_.foo); }

private:
  stored_record & wrapped_;
};
Run Code Online (Sandbox Code Playgroud)

现在的问题是当给定 a 时我们不能使用包装器,const stored_record &因为包装器存储了一个可变引用。我们也不能让它存储非常量引用,因为它无法实现非常量 …

c++ interface constants language-lawyer

10
推荐指数
1
解决办法
328
查看次数

当方法不修改成员时,在const实例上调用非const方法是否为UB?

代码说出了上千个单词,所以...

这是使a发生突变的未定义行为const int

struct foo {
    int x;
    void modify() { x = 3; }
};

void test_foo(const foo& f) {
    const_cast<foo&>(f).modify();
}

int main(){
    const foo f{2};
    test_foo(f);
}
Run Code Online (Sandbox Code Playgroud)

那这个呢:

struct bar {
    void no_modify() { }
};

void test_bar(const bar& b) {
    const_cast<bar&>(b).no_modify();
}

int main(){
    const bar b;
    test_bar(b);
}
Run Code Online (Sandbox Code Playgroud)

const_cast当方法不改变对象时,是否可以通过const对象调用非const方法?

PS:我知道no_modify应该将其声明为const,然后这个问题毫无意义,但假设bars的定义不能更改。

PPS:只需确保:不要在家中(或其他任何地方)这样做。我绝不会让这样的代码通过审查。可以轻松避免强制转换。这是一个语言律师问题。

c++ const const-cast language-lawyer

6
推荐指数
1
解决办法
144
查看次数

shared_ptr析构函数,复制和不完整类型

我有这样的头文件foo.h(省略了无关的东西):

#pragma once
#include <memory>

class Bar;


struct Foo
{
  std::shared_ptr<Bar> getBar();

  std::shared_ptr<const Bar> getBar() const
  {
    return const_cast<Foo*>(this)->getBar();
  }
};
Run Code Online (Sandbox Code Playgroud)

非const重载getBar()在.cpp文件中实现,该文件也可以看到完整定义Bar.

foo.h包含在另一个文件(没有看到定义Bar)时,VS 2010会给我一个这样的警告:

warning C4150: deletion of pointer to incomplete type 'Bar'; no destructor called
Run Code Online (Sandbox Code Playgroud)

在const重载getBar()(或实际上在从该重载实例化的标准库中的深处).

我的问题是这个警告是否可以安全地被忽略.

我看待它的方式,有两个std::shared_ptr<Bar>被调用的成员函数getBar() const:转换构造函数和析构函数.

// converting constructor
template <class Y>
std::shared_ptr<const Bar>::shared_ptr(std::shared_ptr<Y> &&r)
Run Code Online (Sandbox Code Playgroud)

这用于初始化返回值getBar() const的返回值getBar().这并未列出任何先决条件(C++ 1127.2.2.1§20-22),这些先决条件需要Y(Bar在我的情况下)完成.

// destructor
std::shared_ptr<const Bar>::~shared_ptr() …
Run Code Online (Sandbox Code Playgroud)

c++ shared-ptr incomplete-type c++11

5
推荐指数
1
解决办法
1322
查看次数