📜 ⬆️ ⬇️

STL Filter - script for laconic STL errors

STL is renowned for its ability to fill screen space meters with error messages. Tired of looking at the screen and seeing pearls like:

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> >]


Well, a couple more dozen lines.


')
The solution to the problem of long errors is to use STL output filters. About one such filter, stlfilt , told Scott Meyers in his book Effective STL.

Usage example


We have a code with errors in using 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 .


instead of g ++ testmap.cpp, we run gfilt testmap.cpp. Result:
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 *'

In my opinion, it looks much nicer.

Installation


Installation and setup is simple:

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


All Articles