Template tricks and examples

Pages: 123
The error message format VC++ uses is a little friendlier, I think, but not much.


1>c:\projects\junk\junk\main.cpp(7): error C2440: 'initializing' : cannot convert from 'std::_Tree_const_iterator<_Mytree>' to 'std::_Tree_const_iterator<_Mytree>'
1>          with
1>          [
1>              _Mytree=std::_Tree_val<std::_Tset_traits<int,std::less<int>,std::allocator<int>,false>>
1>          ]
1>          and
1>          [
1>              _Mytree=std::_Tree_val<std::_Tset_traits<unsigned int,std::less<unsigned int>,std::allocator<char32_t>,false>>
1>          ]
1>          No constructor could take the source type, or constructor overload resolution was ambiguous
1>c:\projects\junk\junk\main.cpp(7): error C2679: binary '!=' : no operator found which takes a right-hand operand of type 'std::_Tree_const_iterator<_Mytree>' (or there is no acceptable conversion)
1>          with
1>          [
1>              _Mytree=std::_Tree_val<std::_Tset_traits<int,std::less<int>,std::allocator<int>,false>>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\xtree(333): could be 'bool std::_Tree_const_iterator<_Mytree>::operator !=(const std::_Tree_const_iterator<_Mytree> &) const'
1>          with
1>          [
1>              _Mytree=std::_Tree_val<std::_Tset_traits<unsigned int,std::less<unsigned int>,std::allocator<char32_t>,false>>
1>          ]
1>          while trying to match the argument list '(std::_Tree_const_iterator<_Mytree>, std::_Tree_const_iterator<_Mytree>)'
1>          with
1>          [
1>              _Mytree=std::_Tree_val<std::_Tset_traits<unsigned int,std::less<unsigned int>,std::allocator<char32_t>,false>>
1>          ]
1>          and
1>          [
1>              _Mytree=std::_Tree_val<std::_Tset_traits<int,std::less<int>,std::allocator<int>,false>>
1>          ]


I've heard clang's template error reports are fairly readable.
You have to agree that not many people would know that, I'm just currious if there is more such sintactic sugars...

If you have any example please post it here :)


The standard library as well as boost and other libraries are full of this, except it's useful. I think you just picked a poor example.

See iterator_traits or any traits classes. They are all over the place.

I mean, why is the In and Deep templates even templates if the type is always Int? It isn't even specialized.
Last edited on
cire wrote:
I've heard clang's template error reports are fairly readable.


They're nicely formatted, colorful, and may contain relevant code snippets.
Although in this case, not even Clang will tell you that it is unsigned int that is wrong.
Edit: disregard that, it was stupid reasoning.
terror.cpp:7:46: error: no viable conversion from 'iterator' (aka '_Rb_tree_const_iterator<value_type>') to 'std::set<unsigned int>::const_iterator' (aka '_Rb_tree_const_iterator<value_type>')
        for (std::set<unsigned int>::const_iterator ci = s.begin(); ci != s.end(); ++ci);
                                                    ^    ~~~~~~~~~
/usr/bin/../lib64/gcc/x86_64-suse-linux/4.7/../../../../include/c++/4.7/bits/stl_tree.h:227:12: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'iterator' (aka '_Rb_tree_const_iterator<value_type>') to 'const std::_Rb_tree_const_iterator<unsigned int> &' for 1st argument; 
    struct _Rb_tree_const_iterator
           ^
/usr/bin/../lib64/gcc/x86_64-suse-linux/4.7/../../../../include/c++/4.7/bits/stl_tree.h:249:7: note: candidate constructor not viable: no known conversion from 'iterator' (aka '_Rb_tree_const_iterator<value_type>') to 'const iterator &' (aka 'const _Rb_tree_iterator<unsigned int> &') for 1st argument; 
      _Rb_tree_const_iterator(const iterator& __it)
      ^
1 error generated.

Last edited on
Topic archived. No new replies allowed.
Pages: 123