2016-05-11 14 views
0

私は、という2つのタイプの単一のメンバを持つクラスを持っています:vectorの整数と、それを含むクラスオブジェクトのvectorrecursive_wrapperの助けを借りて)。私はTestVariantvectorを作成し、TestVariantの再帰的なインスタンスのインスタンスを据え付けることにより、このクラスを使用しようとすると、私は「アクセスを取得boost :: variant:再帰的なベクトル型で異常な振る舞い

std::vector<TestVariant> v; 

v.emplace_back(std::vector<TestVariant>{ std::vector<TestVariant>{}, std::vector<int>{} }); // access violation! 

次のようにクラスのコードは

#include <boost/variant/variant.hpp> 
#include <boost/variant/recursive_wrapper.hpp> 

#include <vector> 

class TestVariant 
{ 
public: 

    typedef boost::variant< std::vector<int>, boost::recursive_wrapper<std::vector<TestVariant> > > StorageType; 

    TestVariant(): 
     m_value{ std::vector<int>{} } 
    {} 

    TestVariant(const TestVariant& other): 
     m_value{ other.m_value } 
    {} 

    TestVariant& operator=(const TestVariant& other) 
    { 
     m_value = other.m_value; 

     return *this; 
    } 

    TestVariant(TestVariant&& other): 
     m_value{ std::move(other.m_value) } 
    {} 

    TestVariant& operator=(TestVariant&& other) 
    { 
     m_value = std::move(other.m_value); 

     return *this; 
    } 

    TestVariant(const std::vector<int>& value): 
     m_value{ value } 
    {} 

    TestVariant(std::vector<int>&& value): 
     m_value{ std::move(value) } 
    {} 

    TestVariant(const std::vector<TestVariant>& value): 
     m_value{ value } 
    {} 

    TestVariant(std::vector<TestVariant>&& value): 
     m_value{ std::move(value) } 
    {} 

private: 

    StorageType m_value; 
}; 

を下回っていますMSVS 2013とBoost ver 1.53の「違反」の例外(私はこれがBoostの古いバージョンであることを知っていますが、現時点ではこれを使用するように組織的に制約されています)。 coliruの同じコードがうまく動作します(here参照)。

何でも見知らぬ人であることは

std::vector<TestVariant> v; 

v.emplace_back(std::vector<TestVariant>{std::vector<int>{}, std::vector<TestVariant>{} }); // works! 

MSVS 2013年に、私は2つのベクトルの順序を切り替えた場合、すべてが正常であると考えられるということである任意のアイデア?私は今これを解決しようとしています...

+1

それはwouldn MSVS2013または古いバージョンのバグがあることを見つけるのは珍しいことです。これはあなたの組織が正しいことをしてアップグレードするべきだと主張するよい機会かもしれません。 –

+0

@RichardHodges私はもっと同意できませんでしたが、コグはゆっくり回り、その間に回避策が必要です... –

+0

seheの解答ヘルプですか?私はそれを試してみたいと思うが、私のWindowsコンピュータにvs2015をインストールするには2日かかってしまったので、私は並行インストールを試していない。最近、Windows用のコードを記述する必要がある場合は、clangまたはCygwinを使用します。私はちょうどMicrosoftを信頼していない... –

答えて

0

これは確かに均一な初期化/初期化子リストを持つ多くのバグの1つと思われます。コードは、それは私がに走ったバグに似たものなら、あなたが明示的に型名を指定しようとすることができ

(つまりUBをトリガしないという意味で)「ファイン」である:

v.emplace_back(std::vector<TestVariant>{ 
    TestVariant(std::vector<TestVariant>{}), 
    TestVariant(std::vector<int>{}) }); 
+0

それは残念ながら、動作しません。 –

関連する問題