例如,
#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)
我能保证这种行为会发生在任何符合标准的编译器上吗?
我有一个相当大的类,它被减少到下面的最小失败示例:
#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)