小编seg*_*ult的帖子

C++ 标准是否保证有关在空容器上运行的算法的任何内容?

例如,

#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>

int main() {
  std::vector<int> empty{};
  std::reverse(empty.begin(), empty.end());
  std::cout << "Sum: " << std::accumulate(empty.cbegin(), empty.cend(), 0) << std::endl;
  std::cout << empty.size();
}
Run Code Online (Sandbox Code Playgroud)

按照我的预期构建和运行:

sum: 0
size: 0
Run Code Online (Sandbox Code Playgroud)

我能保证这种行为会发生在任何符合标准的编译器上吗?

c++ std undefined-behavior

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

派生模板类找不到基模板类的移动构造函数

我有一个相当大的类,它被减少到下面的最小失败示例:

#include <vector>

template <typename T> class Base {
public:
  Base(std::vector<T> &&other) : map{other} {}

private:
  const std::vector<T> map;
};

template <typename T> class Derived : public Base<T> {
public:
  Derived(std::vector<T> &&other) : Base<T>{other} {}
};

int main() {
  Derived<double>(std::vector<double>{1,2,3});
}
Run Code Online (Sandbox Code Playgroud)

当我运行这个时,我得到

$ clang++ -std=c++17 -O3 main.cpp && ./a.out
main.cpp:13:37: error: no matching constructor for initialization of 'Base<double>'
  Derived(std::vector<T> &&other) : Base<T>{other} {}
                                    ^      ~~~~~~~
main.cpp:17:3: note: in instantiation of member function 'Derived<double>::Derived' requested here
  Derived<double>(std::vector<double>{1,2,3});
  ^
main.cpp:3:29: note: candidate …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance templates c++17

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

标签 统计

c++ ×2

c++17 ×1

inheritance ×1

std ×1

templates ×1

undefined-behavior ×1