3

マークされたコンパイラ エラー (C2899) が表示されるのはなぜですか? 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: で使用できることはわかってstd::copy(..)いますstd::back_inserter(..)が、それは重要ではありません。

編集

質問は perreal のコメントで回答されました: http://connect.microsoft.com/VisualStudio/feedback/details/694857/bug-in-lambda-expressions

編集

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

4

4 に答える 4

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); 
} 
于 2012-02-27T14:44:14.617 に答える
1

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

古い g++ 4.3 でもコンパイルできます。したがって、おそらく MS コンパイラのバグです。

于 2012-02-27T14:21:11.107 に答える
0

I may be misunderstanding what you are trying to achieve but shouldn't the line in question simply be:

target.push_back(element);

于 2012-02-27T14:17:09.947 に答える
0

C++0x/11 を使用しているため、以下を使用できます。

target.push_back( (decltype(element))(element)); 
于 2012-02-27T14:24:02.027 に答える