小编Гео*_*нов的帖子

如何避免 C++17 中的虚拟继承?

让我们看看示例类。基类是ITransport,传输类接口:

class ITransport {
  public:
    virtual void move(const Path& p) = 0;
    virtual double estimateTime(const Path& path) = 0;
    /*Some more methods.*/
};
Run Code Online (Sandbox Code Playgroud)

执行:

class Transport : public ITransport { 
  public:
    virtual void move(const Path& p) override {
        currPoint_ = p.lastPoint(); 
    }
    /*Some more methods.*/
  private:
    Point currPoint_;
};
Run Code Online (Sandbox Code Playgroud)

我们还假设我们要创建一个自移动的运输类:

template <typename EnergySource>
class SelfMovingTransport : public Transport {
  /*Some special methods for self moving transport.*/
};

Run Code Online (Sandbox Code Playgroud)

自动运输最简单的例子是汽车:

template <typename EnergySource>
class Car : public SelfMovingTransport <EnergySource> { …
Run Code Online (Sandbox Code Playgroud)

c++ multiple-inheritance rtti c++17

7
推荐指数
2
解决办法
326
查看次数

调用不完整类型的成员函数在 gcc 中编译,但在 clang 和 msvc 中不编译

这是一个例子:

struct TestClass {
  void testFunction() const {}
  static TestClass* ptr_;
  constexpr static auto funcPtr = +[](){ ptr_->testFunction(); };
};

TestClass* TestClass::ptr_ = new TestClass();

int main() {
  TestClass::funcPtr();
  delete TestClass::ptr_;
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

这会在gccand 中编译,不会在clangand中编译msvc。哪个编译器是正确的?这是一个错误吗gcc

这是clang错误消息:

<source>:4:46: error: member access into incomplete type 'TestClass'
    4 |   constexpr static auto funcPtr = +[](){ ptr_->testFunction(); };
      |                                              ^
<source>:1:8: note: definition of 'TestClass' is not complete until the closing '}'
    1 …
Run Code Online (Sandbox Code Playgroud)

g++ visual-c++ language-lawyer clang++ c++17

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

连接向量范围并没有给出范围

我正在尝试对笛卡尔积进行变换,最终使边缘变平。我收到的结果不是范围,这就是问题所在:

#include <ranges>
#include <vector>
#include <iostream>
#include <tuple>
#include <iterator>

int main() {
    auto indicies = std::views::iota(0, 3);

    auto coords = std::views::cartesian_product(indicies, indicies);

    auto edges = coords | std::views::transform([](const auto& coord) {
        using Coord = std::pair<std::size_t, std::size_t>;
        using Edge = std::pair<Coord, Coord>;
        std::vector<Edge> ret;
        const auto& [x, y] = coord;
        auto curr = Coord{x, y};
        if (x + 1 < 3) {
            auto dest = Coord{x + 1, y};
            ret.emplace_back(curr, dest);
        }
        if (y + 1 < 3) { …
Run Code Online (Sandbox Code Playgroud)

c++ gcc std-ranges c++23

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