STLフィルター-簡潔なSTLエラーのスクリプト

STLは、画面スペースのメーターにエラーメッセージを表示する機能で有名です。 画面を見て、次のような真珠を見るのにうんざりしています:

testmap.cpp:25: error: no matching function for call to 'std::map<int, double, std::less, std::allocator<std::pair<const int, double> > >::map(int, int, int)'
/usr/include/c++/4.3/bits/stl_map.h:175: note: candidates are: std::map<_Key, _Tp, _Compare, _Alloc>::map(const std::map<_Key, _Tp, _Compare, _Alloc>&) [with _Key = int, _Tp = double, _Compare = std::less, _Alloc = std::allocator<std::pair<const int, double> >]
/usr/include/c++/4.3/bits/stl_map.h:165: note: std::map<_Key, _Tp, _Compare, _Alloc>::map(const _Compare&, const _Alloc&) [with _Key = int, _Tp = double, _Compare = std::less, _Alloc = std::allocator<std::pair<const int, double> >]


さて、さらに数十行。



長いエラーの問題の解決策は、STL出力フィルターを使用することです。 このようなフィルターの1つであるstlfiltは 、Scott Meyersの著書Effective STLで説明されています。

使用例


std :: mapの使用にエラーがあるコードがあります:
#include <iostream>
#include <map>
#include <algorithm>
#include <cmath>
using namespace std;

const int values[] = { 1,2,3,4,5 };
const int NVALS = sizeof values / sizeof ( int );

struct intComp: public binary_function< int , int , bool >
{
bool operator ()( int a, int b) const
{
return a < b;
}
};

int main()
{
using namespace std;

typedef map< int , double > valmap;
typedef map< int *, double *> pmap;

valmap m2(1,2,3);
pmap m3(1,2,3);
map< int , double , intComp> valmap3;

valmap m;
pmap p;

for ( int i = 0; i < NVALS; i++)
{
m.insert(make_pair(values[i], pow(values[i], .5)));
valmap3.insert(0);
}

valmap::iterator it = 100;
valmap::const_iterator cit = 100;

m.insert(1,2);
m.insert(make_pair(36, 10.57)); // fine, more convenient
m.insert(m.end(), make_pair(40, 2.29)); // also fine
return 0;
}

* This source code was highlighted with Source Code Highlighter .


g ++ testmap.cppの代わりに、gfilt testmap.cppを実行します。 結果:
testmap.cpp: In function 'int main()':
testmap.cpp:25: error: No match for 'map<int, double>::map(int, int, int)'
testmap.cpp:26: error: No match for 'map<int *, double *>::map(int, int,
int)'
testmap.cpp:35: error: No match for 'map<int, double, intComp>::insert(
int)'
testmap.cpp:38: error: conversion from 'int' to non-scalar type 'gen_map<
int, double>::iterator' requested
testmap.cpp:39: error: conversion from 'int' to non-scalar type 'gen_map<
int, double>::const_iterator' requested
stl_tree.h: In member function 'void map<
int, double>::_M_insert_unique(_II, _II)':
[STL Decryptor: Suppressed 1 more STL standard header message]
testmap.cpp:41: instantiated from here
stl_tree.h:1295: error: invalid type argument of 'unary *'

私の意見では、それはずっと良く見えます。

設置


インストールとセットアップは簡単です。

Source: https://habr.com/ru/post/J74663/


All Articles