2

このスニペットの最後の行にタイプミスがあります(修飾子:: typeがありません)。

template<bool ci> struct comp        { typedef ci_compare_string      type; };
template<       > struct comp<false> { typedef std::less<std::string> type; };

template <typename T, bool ci = true>  //map w str keys, case sensitive option
struct mapx : std::map<std::string, T, typename comp<ci> > {};  // oops, should be comp_<ci>::type

VS 2008コンパイラは、以下に示すstd::mapソース行でエラーを報告しました。メッセージは「termは2つの引数を取る関数に評価されません」でした。

...
mapped_type& operator[](const key_type& _Keyval)
    {   // find element matching _Keyval or insert with default mapped
    iterator _Where = this->lower_bound(_Keyval);
    if (_Where == this->end()
        || this->comp(_Keyval, this->_Key(_Where._Mynode())))   <=== ERROR !!!!
        _Where = this->insert(_Where,
            value_type(_Keyval, mapped_type()));
    return ((*_Where).second);
    }
};
...

結局、エラーはコンパレータに関係しているに違いないと考え、「::type」と入力するのを忘れていることに気付くまでじっと見つめていました。

私はこれまで多くのwテンプレートを使用したことがなく、このようなコンパイラエラーを追跡する正しい方法を知りたいと思います。このタイプの状況で使用することになっているヒント/トリックはありますか?

4

1 に答える 1

4

メッセージは「termは2つの引数を取る関数に評価されません」でした

Visual Studioでは、エラーリストにはエラーメッセージの最初の行のみが表示されます。これは、エラーの概要を一目で確認できるようにすることを目的としています。一部のエラーメッセージは、特にテンプレートが関係している場合は非常に長くなります。完全なエラーメッセージは、ビルドの[出力]ウィンドウに表示されます。

テンプレートのインスタンス化中にエラーが発生すると、コンパイラーは、エラーが検出されたときにインスタンス化していたテンプレートのスタックを出力します。たとえば、スニペットをVisual C ++ 2012でコンパイルすると、次のエラーが出力されます(Visual C ++ 2008でも同様のメッセージが出力されますが、標準ライブラリの実装が異なるため、必ず異なります)。

C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\xtree(1792) : error C2064: term does not evaluate to a function taking 2 arguments
        C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\xtree(1153) : see reference to function template instantiation 'std::pair<_Ty1,_Ty2> std::_Tree<_Traits>::_Insert_nohint<std::pair<const _Kty,_Ty>&,std::_Tree_node<_Value_type,_Voidptr>*>(bool,_Valty,_Nodety)' being compiled
        with
        [
            _Ty1=std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const std::string,int>>>>,
            _Ty2=bool,
            _Traits=std::_Tmap_traits<std::string,int,comp<true>,std::allocator<std::pair<const std::string,int>>,false>,
            _Kty=std::string,
            _Ty=int,
            _Value_type=std::pair<const std::string,int>,
            _Voidptr=void *,
            _Valty=std::pair<const std::string,int> &,
            _Nodety=std::_Tree_node<std::pair<const std::string,int>,void *> *
        ]
        C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\xtree(1153) : see reference to function template instantiation 'std::pair<_Ty1,_Ty2> std::_Tree<_Traits>::_Insert_nohint<std::pair<const _Kty,_Ty>&,std::_Tree_node<_Value_type,_Voidptr>*>(bool,_Valty,_Nodety)' being compiled
        with
        [
            _Ty1=std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const std::string,int>>>>,
            _Ty2=bool,
            _Traits=std::_Tmap_traits<std::string,int,comp<true>,std::allocator<std::pair<const std::string,int>>,false>,
            _Kty=std::string,
            _Ty=int,
            _Value_type=std::pair<const std::string,int>,
            _Voidptr=void *,
            _Valty=std::pair<const std::string,int> &,
            _Nodety=std::_Tree_node<std::pair<const std::string,int>,void *> *
        ]
        stubby.cpp(12) : see reference to function template instantiation 'std::pair<_Ty1,_Ty2> std::_Tree<_Traits>::insert<std::pair<const char *,int>>(_Valty &&)' being compiled
        with
        [
            _Ty1=std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const std::string,int>>>>,
            _Ty2=bool,
            _Traits=std::_Tmap_traits<std::string,int,comp<true>,std::allocator<std::pair<const std::string,int>>,false>,
            _Valty=std::pair<const char *,int>
        ]
        stubby.cpp(12) : see reference to function template instantiation 'std::pair<_Ty1,_Ty2> std::_Tree<_Traits>::insert<std::pair<const char *,int>>(_Valty &&)' being compiled
        with
        [
            _Ty1=std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const std::string,int>>>>,
            _Ty2=bool,
            _Traits=std::_Tmap_traits<std::string,int,comp<true>,std::allocator<std::pair<const std::string,int>>,false>,
            _Valty=std::pair<const char *,int>
        ]
C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\xtree(1796) : error C2064: term does not evaluate to a function taking 2 arguments
C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\xtree(1817) : error C2064: term does not evaluate to a function taking 2 arguments

さて、これらの用語の定義によって、これはまだ特に読みやすく、ユーザーフレンドリーではありません。ただし、エラーが発生した場所は表示されます。3つのトップレベルエラーは、の1792、1796、および1817行で発生し<xtree>、これらの行はすべて、比較子を使用して2つの引数を比較しようとします。

于 2013-02-22T23:14:01.020 に答える