This MCVE:
#include <stdio.h>
#include <time.h>
#define MAX_LENGTH_DATETIME 25
template <typename T>
char * convertUnixTimeToChar( time_t unixTime, T )
{
(void) unixTime;
#pragma message "2nd parameter in convertUnixTimeToChar() must be of type <char [MAX_LENGTH_DATETIME]>."
exit(1);
};
template<size_t N>
char * convertUnixTimeToChar( time_t unixTime, char (&destination) [N] )
{
if ( N < MAX_LENGTH_DATETIME )
{
printf( "Overflow in convertUnixTimeToChar(): destination size [%ld] must be at least [%u]", N, MAX_LENGTH_DATETIME );
exit(1);
}
struct tm * tmNow;
tmNow = localtime( &unixTime );
strftime( destination, MAX_LENGTH_DATETIME - 1, "%Y-%m-%d %H:%M:%S", tmNow );
return destination;
};
int main()
{
char buffer [MAX_LENGTH_DATETIME ];
printf( "Converted unix time=%s
", convertUnixTimeToChar( 1487585045, buffer ) );
}
My question:
I would like to get displayed the message #pragma message "2nd parameter in convertUnixTimeToChar() must be of type <char [MAX_LENGTH_DATETIME]>."
from gcc while the compilation only when a variable of type char *
has been passed to convertUnixTimeToChar()
because the 1st template was resolved. Now I get always this message.
In other words, the compilation should fail if a wrong parameter is passed and not when the program is running [in this case I have to use a printf
instead of #pragma
to get notified].
Passing something like char [25]
resolves the 2nd template and all is fine!
Is there a way I can stop conditionally the compilation with an error message in case a template has been used physically for the code generation?
Compiled with gcc 4.9.4 using these switches: -Wall -Werror -Wextra -std=c++11 -O3
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…