Discussion: function trace
Afficher un message
Vieux 17/10/2007, 07h29   #8
baibaichen
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: function trace

On Oct 16, 10:31 pm, wijnand <wijnandsuij...@gmail.com> wrote:
> Is this something you can use?
>
> #include <iostream>
> #include <string>
>
> template <class T>
> struct ToString;
> // keep this empty to emit a compile time error
> // when it doesn't have a associated string
>
> // Define the format specifiers of the basic types
> #define DEF0TYPE( type, str ) \
> template <> \
> struct ToString< type > \
> { static std::string value() { return str; } \
> };
>
> DEF0TYPE( int, "%d" )
> DEF0TYPE( double, "%f" )
> DEF0TYPE( char *, "%s" )
> DEF0TYPE( const char *, "%s" )
> DEF0TYPE( float, "%f")
> DEF0TYPE( char, "%c")
> /* andsoforth... */
> #undef DEF0TYPE
>
> // define the format string of a nullary function
> template <class Return>
> struct ToString< Return (*) () >
> {
> static std::string value() { return "%s()"; }
>
> };
>
> // unary functions
> template <class Return, class T >
> struct ToString< Return (*) ( T ) >
> {
> static std::string value() { return std::string("%s(") +
> ToString<T> :: value() + ")"; }
>
> };
>
> // binary functions
> template <class Return, class T, class U >
> struct ToString< Return (*) ( T , U ) >
> {
> static std::string value() { return std::string("%s(") +
> ToString<T> :: value() + ", " + ToString<U>::value() + ")"; }
>
> };
>
> // ternary functions
> template <class Return, class T, class U, class V >
> struct ToString< Return (*) ( T , U, V ) >
> {
> static std::string value() { return std::string("%s(") +
> ToString<T> :: value() + ", " + ToString<U>::value() + ", " +
> ToString<V>::value() + ")"; }
>
> };
>
> // and so on
>
> // a er function, which provides a bit nicer syntax
> template <class T>
> std::string toString( T )
> {
> return ToString<T> :: value();
>
> }
>
> // two functions
> void function1( int a, double b) {}
> void function2( double x, double y, char * string) {}
>
> int main()
> {
> std::cout << "format string for the main function: " <<
> toString( main ) << std::endl;
> std::cout << "format string of 'function1' " <<
> toString( function1 ) << std::endl;
> std::cout << "format stirng of 'function2' " << toString( function2)
> << std::endl;
> return 0;
>
> }
>
> This gives as output
> format string for the main function: %s()
> format string of 'function1' %s(%d, %f)
> format stirng of 'function2' %s(%f, %f, %s)
>
> regard,
> Wijnand Suijlen


Thanks, it does me.
but it's in run-time, not compile time.
Is there any imporvement?

chang

  Réponse avec citation
 
Page generated in 0,05801 seconds with 9 queries