Ela*_*782 5 c++ stl visual-studio c++11 visual-studio-2015
我遇到了一个奇怪的问题,它只在Release模式下显示,但没有在Debug模式下(使用VS2015 RC).Aggregate初始化的第二个类成员使用与第一个相同的值,即使它应该默认初始化.
更为奇特的,有一次我改变Base() = default;到Base() {};的古怪行为也没有了.但它们应该是等价的,不应该吗?我找到了VS bug吗?
我能够把它归结为:
#include <iostream>
#include <vector>
#include <random>
using namespace std;
class Base {
public:
Base() = default; // elicits the odd behaviour on VS Release mode
//Base() {}; // works
Base(std::vector<int> m) : m(m) {};
size_t get_m() const { return m.size(); };
private:
std::vector<int> m;
std::mt19937 engine;
};
class Aggregate {
public:
Aggregate() = default;
Aggregate(Base one, Base two) : one(one), two(two) {};
Base get_one() const { return one; };
Base get_two() const { return two; };
private:
Base one;
Base two;
};
Aggregate make_aggregate() {
Base b1(std::vector<int> { 1, 2 });
Base b2(std::vector<int> { 3, 4, 5});
return Aggregate(b1, b2);
};
int main() {
Aggregate a1 = make_aggregate();
Base base = Base();
Aggregate a2(a1.get_one(), base); // a2.two.m should be empty and it is
Aggregate a3(a1.get_one(), Base()); // a2.two.m should be empty but it's set to a2.one.m
// should print '2, 0', and it does:
cout << a2.get_one().get_m() << ", " << a2.get_two().get_m() << endl;
// prints '2, 2' on VS in Release mode! (and '2, 0' in Debug mode, and on clang):
cout << a3.get_one().get_m() << ", " << a3.get_two().get_m() << endl;
}
Run Code Online (Sandbox Code Playgroud)
我在这里未定义的行为(例如,mt19937 必须是静态的吗?)或我的代码是否正常?这是一个VS bug吗?clang-3.5使用和不使用代码都很好-g.
| 归档时间: |
|
| 查看次数: |
178 次 |
| 最近记录: |