How to print a complete template argument at compile time in C ++

Let's say that I applied the template class as follows:

template <size_t N> class C 
{
     void f()
     {
        // print out N here?
     }
};

I want the compiler to compile a sentence like

C<20> c;

he will display a message

"class C template with N = 20"

I tried C # pragma and static_assert in vain.

The problem is

  • With # pragma and static_assert, I could not embed the integral (20 here) in the message;
  • with preprocessors, it is too early that N is not replaced by 20 so far.

Is there a way or not?

Thanks.

+3
source share
7 answers

, (). , GNU :

make
nm foo | c++filt | grep 'C<[^>]\+>::f'

foo - .

, , , , class C.

:

grep '<[^>]\+>::'

, , , STL iostream . !

+3

, , . , ++, , .

+1

Dan .

, , , , :

template <int N> class C
{
public:
  C ()
  {
    int d1;
    int d1 = d1;  // Using uninitialized variable - warning
  }
};

C<10> c;

g++ -Wuninitialized, :

t.cc: In constructor 'C<N>::C() [with int N = 10]':
t.cc:7: warning: 'i' is used uninitialized in this function

MACRO .

+1

, N == 20?

, ?

template <> class C <20> 
{
     int class_C_is_templated_with_20[-1];
}
0

:

Test.cpp: 'C < 20 > :
Test.cpp: 14:
Test.cpp: 9: :

"Assertion_failed (MPL _:: ************
(C < 20 > :: CLASS_C_TEMPLATED_WITH_I_EQUAL_TO _:: ************) (mpl _:: int_ < 20 > ))

, . , , .

#include <boost/mpl/assert.hpp>
#include <boost/mpl/int.hpp>

template<int i_>
class C
{
public:

BOOST_MPL_ASSERT_MSG( false, CLASS_C_TEMPLATED_WITH_I_EQUAL_TO_, (boost::mpl::int_<i_>) );

};

int main() {
C<20>();
}
0

, (, f). , , , N, ( ) . , .

, - , , ( ). , static_assert GCC.

0

, , , ( , ). , :

template <typename T>                
inline void debug_type(const T&) __attribute__((deprecated));        

template <typename T>                                          
inline void debug_type(const T&) { }                           

template <typename T>                                        
inline void debug_type() __attribute__((deprecated));        

template <typename T>                                            
inline void debug_type() { } 

:

debug_type(1); // Pass a value, let the compiler deduce its type
debug_type<char>(); // Pass a type explicitly

:

foo.cpp:73:17: warning: 'void debug_type(const T&) [with T = int]' is deprecated (declared at /tmp/arduino_build_static/sketch/Num.h:13) [-Wdeprecated-declarations]
     debug_type(1);
                 ^
foo.cpp:74:22: warning: 'void debug_type() [with T = char]' is deprecated (declared at /tmp/arduino_build_static/sketch/Num.h:19) [-Wdeprecated-declarations]
 debug_type<char>();

T = int ( , , , ).

A special advantage of this warning in warning of an unused variable, suggested elsewhere, is that the place of the error is the place where it is called debug_type, and not its implementation, therefore the code fragment shown in the error shows an expression of the type (which may be convenient when you want to print several different at the same time).

0
source

Source: https://habr.com/ru/post/1724378/


All Articles