How to trace/debug if g++ doesn’t like your templates based code

#include <typeinfo>
#include <cxxabi.h>
#include <cstdlib>
#include <memory>
#include <string>

template <typename Type>
std::string TypeName()
{
    //https://en.wikipedia.org/wiki/Name_mangling
    //https://gcc.gnu.org/onlinedocs/libstdc++/libstdc++-html-USERS-4.3/a01696.html

    auto demangle=[](char const* mangled) -> std::string {
        auto ptr = std::unique_ptr<char, decltype(& std::free)>{
            abi::__cxa_demangle(mangled, nullptr, nullptr, nullptr),
            std::free
        };
        return {ptr.get()};
    };
    return demangle(typeid(Type).name());
}
//...
template <typename F, typename ...Args>
void //typename std::enable_if<std::is_same<...   <---------------------if something worng is here
foo()
{
    std::cout << "line: "<< __LINE__ <<": "<<TypeName<F(Args...)>() << std::endl;
    std::cout << "line: "<< __LINE__ <<": "<<TypeName<F(char,Args...)>() << std::endl;
    std::cout << "line: "<< __LINE__ <<": "<<TypeName<typename std::result_of<F(char,Args...)>::type>() << std::endl;
}

#c++ #templates

Published by moon_man

34 y.o. human from the Moon

Leave a comment

Your email address will not be published. Required fields are marked *