Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a template debugger?

Templates can be programs in themselves.

Is there a template debugger so you can step thru the "execution" of the template?

This would basically have to be something that is done during compile/link/codegen - and is distinct from debugging the generated program.

Even in many "primitive" environments where you cannot use a debugger, you can usually do "printf debugging". Is even that possible with templates?

edit: Another way to think about this is something like the C preprocessor. It is often very useful to generate "preprocessed" source code - the output from the preprocessor that the compiler is actually compiling - this lets you see what effect your macros are having. A template equivalent would be great - have the compiler output the non-template source code which cooresponds to the templated input. The closest you can get I suppose is a C++ to C translator. (Doesn't the comeau compiler do this?)

like image 795
joeking Avatar asked Oct 14 '14 16:10

joeking


2 Answers

You might want to look at this patch for clang that outputs template instantiations.

Another simple tool is the error messages your compiler produces for attempting to instantiate an undefined template.

template< typename > struct TD;

template< typename T >
void your_template_function( T & param )
{
    // Both of these produce an error about "undefined type TD< T > with T = ..."
    TD< T > test1;
    TD< decltype( param ) > test2;
}

This is explained in Scott Meyers CPPCon talk, right after the ring-tailed lemur slide.

like image 190
Khouri Giordano Avatar answered Oct 13 '22 19:10

Khouri Giordano


On the last years c++ conference there was a talk to that topic. Some of the informations you can find here:

http://gsd.web.elte.hu/contents/articles/gpce06.pdf

and

http://patakino.web.elte.hu/ECOOP_Templight_Poster.pdf

I have no idea how functional that stuff now is, but it was a very interesting startpoint.

I personal wrote me some helper classes which are able to print me the given types like printf debugging for standard code. If the compilation fails it often gives a good error message while calling the DebugPrinter and if the program compiles but the result is really stupid because the type expansion is not what I expect the DebugPrinter helps me a lot!

    template< typename T>
int DebugPrintArgs( T arg )
{
    std::cout << arg  << ", ";
    return 0;
}

template <typename Head, typename ... T>
class DebugPrinter: public DebugPrinter<T...>
{
    public:
        DebugPrinter()
        {
            std::cout << "--------------------------" << std::endl;
            std::cout << __PRETTY_FUNCTION__ << std::endl;
            std::cout << "--------------------------" << std::endl;
        }

        template< typename ...Y>
            DebugPrinter( Y ... rest )
            {
                std::cout << "Construction of: " << __PRETTY_FUNCTION__ << " Values: " ;
                ExpandWithConstructor{DebugPrintArgs( rest)...};
                std::cout << std::endl;
            }

};


template <typename Head>
class DebugPrinter< Head >
{
    public:
        DebugPrinter()
        {
            std::cout << "--------------------------" << std::endl;
            std::cout << __PRETTY_FUNCTION__ << std::endl;
            std::cout << "--------------------------" << std::endl;
        }
};
like image 36
Klaus Avatar answered Oct 13 '22 18:10

Klaus