In the following code, on Line 30, if the object Data is initialized with curly braces, Data{std::move(e)}, GCC compiles, but created executable is giving segmentation fault.
But if the object is initialized C++98 style, Data(std::move(e)), the executable is running as expected.
It seems Clang compiler doesn't have this problem.
Is this a compiler bug or is my code incorrect?
#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>
#include <limits>
#include <math.h>
#include <memory>
#include <assert.h>
#include <sstream>
template <typename T>
void Draw(ostream& os, const T& Entry) {
os << Entry << endl;
}
struct TObject {
template<typename T>
TObject(T e) : Self(make_shared<TModel<T>>(move(e))) {}
friend void Draw(ostream& os, const TObject& Entry) {
Entry.Self->ModelDraw(os);
}
private :
struct TConcept {
virtual ~TConcept() = default;
virtual void ModelDraw(ostream& os) const = 0;
};
shared_ptr<const TConcept> Self;
template<typename T>
struct TModel final : TConcept {
TModel(T e) : Data { move(e) } { }
// It seems the issue is in this line, initiliazer list C++11 version does not work
// TModel(T e) : Data ( move(e) ) { }
// C++98 style is working.
void ModelDraw(ostream& os) const {
Draw(os, Data);
}
T Data;
};
};
class TMyClass {
public :
string s;
TMyClass(string e) : s(move(e)) {};
friend void Draw(ostream& os, const TMyClass& E) {
os << E.s << endl;
}
};
using TDocument = vector<TObject>;
void Draw(ostream& os, const TDocument& vDoc) {
os << " Document Print ------------------" << endl;
for(const auto& e: vDoc)
Draw(os,e);
os << endl;
}
using THistory = vector<TDocument>;
TDocument& GetCurrent(THistory& vHstry) {
return vHstry.back();
}
void SaveHistory(THistory& vHstry) {
vHstry.push_back(vHstry.back());
}
int main(int argc, char** argv) {
TDocument vDoc;
THistory vHstry(1);
GetCurrent(vHstry).emplace_back(1);
GetCurrent(vHstry).emplace_back(3.2);
GetCurrent(vHstry).emplace_back(" This is a try");
GetCurrent(vHstry).emplace_back(" My Class ");
GetCurrent(vHstry).emplace_back(GetCurrent(vHstry));
Draw(cout , GetCurrent(vHstry));
return 0;
}
Compiler flags: -Wall -std=c++11 -O0 -g -o a.out source_file.cpp
Aucun commentaire:
Enregistrer un commentaire