Showing posts with label templates. Show all posts
Showing posts with label templates. Show all posts

Thursday, December 18, 2014

I don't know C++ any more

C++11 seems to be a completely new beast. It also seems to be becoming more like Perl in that there's more than one way (now) to do it (TIMTOWTDI, read "tim-toady"). And this can be quite obfuscatory. Case in point:



#include <iostream>
#include <type_traits>

int main()
{
        typedef std::integral_constant<int, 2> two_t;
        two_t::type::type::type second;
        std::cout << std::boolalpha;
        std::cout << std::is_same<decltype(second), two_t>::value << std::endl;
        std::cout << std::is_same<decltype(second)::type, two_t>::value << std::endl;

        return 0;
}



The stuff highlighted in red above is superfluous, but completely legal per the syntax. And there are several more plays on this to really write obfuscated code.

Wednesday, January 8, 2014

C++ name demangling

C++ meta-programming is great, except when you need to debug compiler errors related to templatized code.

e.g, i got the following error

duplicate symbol __ZlsRNSt3__113basic_ostreamIcNS_11char_traitsIcEEEERKN7myclass7myfunctE in file.o

What does this gobbledegook mean?

c++filt to the rescue!

c++filt __ZlsRNSt3__113basic_ostreamIcNS_11char_traitsIcEEEERKN7myclass7myfunctE

operator<<(std::__1::basic_ostream >&, myclass::myfunct const&)

so, I have a stream insertion operator that's multiply defined! On to the next clue, Watson!