2012-02-27 8 views
3

マークされたコンパイラエラー(C2899)が表示されるのはなぜですか?私はVS2010 SP1を試しました。私は私がstd::back_inserter(..)std::copy(..)を使用することができます知っている - それはポイントではありません。VS2010 SP1を使用した関数テンプレートのコンパイラエラー

#include <list> 
#include <vector> 
#include <algorithm> 

template <typename source_container_type, typename target_container_type> 
void copy_all(const source_container_type& source, target_container_type& target) 
{ 
    std::for_each(begin(source), end(source), [&] (const typename source_container_type::value_type& element) 
    { 
     // error C2899: typename cannot be used outside a template declaration 
     // error C2653: 'target_container_type' : is not a class or namespace name 
     target.push_back(typename target_container_type::value_type(element)); 
    }); 
} 

int main() 
{ 
    std::vector<int> a; 
    a.push_back(23); 
    a.push_back(24); 
    a.push_back(25); 

    std::list<int> b; 
    copy_all(a, b); 
} 

種類は
サイモン

PSについて。質問がperrealでコメントに答えた

EDIT

http://connect.microsoft.com/VisualStudio/feedback/details/694857/bug-in-lambda-expressions

EDIT

私は回避策に興味がないんだということに注意してください。上記のコードがコンパイルされるべきかどうかを知りたい

+4

これはバグのようです:http://connect.microsoft.com/VisualStudio/feedback/details/694857/bug-in-lambda-expressionsあなたのコードはg ++ 4.6.2で動作します – perreal

+0

ラムダはここで何らかの役割を持っています。 –

+0

問題のある型をtypedefすると役に立ちます。 'typedef typename target_container_type :: value_type target_vt; [...] target.push_back(target_vt(element)); 'が動作します。 –

答えて

0

私はあなたが達成しようとしているものを誤解することができるが、問題の行は、単純ではありません。

target.push_back(要素)。

+1

target_container_type :: value_typeに明示的なコンストラクタがある場合は、これが必要です。 – Simon

1

あなたの行は有効です:http://ideone.com/qAF7r

でも古代G ++ 4.3は、それをコンパイルします。だからおそらくMSコンパイラのバグでしょう。

+0

それは本当にバグです。質問の私の編集を見てください。 – Simon

0

あなたがC++ 0xの/ 11を使用しているので、あなたが使用することができます。

target.push_back((decltype(element))(element)); 
+0

これは役に立たないでしょう... – Nim

+0

そして、どうしてですか?私はコンパイルしようとし、それは動作します。 – Ajay

+0

私はただideoneで試してコンパイルしませんでした。実際、コンパイラごとに違うコンプライアンスの問題があります。たとえば。グローバルstd :: beginとstd :: endを使用すると、g ++はコンパイルされません。 – Jagannath

1

申し訳VS2010を用意していません。ラムダの外側でtypedefを動かしてみてください。 g ++で動作します。

#include <list> 
#include <vector> 
#include <algorithm> 

template <typename source_container_type, typename target_container_type> 
void copy_all(const source_container_type& source, target_container_type& target) 
{ 
    typedef typename target_container_type::value_type TargetType; /// Code change here. 

    std::for_each(source.begin(), source.end(), [&] (const typename source_container_type::value_type& element) 
    { 
     target.push_back(TargetType(element)); 
    }); 
} 

int main() 
{ 
    std::vector<int> a; 
    a.push_back(23); 
    a.push_back(24); 
    a.push_back(25); 

    std::list<int> b; 
    copy_all(a, b); 
} 
+0

私は回避策に興味がないことに注意してください - 疑問のあるコードがエラーを出す理由は興味があります。 – Simon

関連する問題