Is template without function parameter list OK?

N

Nephi Immortal

I use template to replace assert macro. I don’t have to provide
function parameter list if I want. I use Static_Data class to access
data members through template parameter.
If you set ASSERT to false in either debug mode or release mode, the
code is omitted from inserting into main function.
Is my code OK and readable? I need to change some names to describe
good readability.

static const bool ASSERT = false;

struct Static_Data
{
static const char a = 1;
static const char b = 2;
static const char c = 3;
};

template< typename t_Static_Data >
bool Do()
{
printf( "Assert Message: %d %d %d\n", t_Static_Data::a,
t_Static_Data::b, t_Static_Data::c );
return true;
}

int main()
{
ASSERT && Do< Static_Data >();

return 0;

}
 
V

Victor Bazarov

I use template to replace assert macro. I don’t have to provide
function parameter list if I want. I use Static_Data class to access
data members through template parameter.
If you set ASSERT to false in either debug mode or release mode, the
code is omitted from inserting into main function.
Is my code OK and readable? I need to change some names to describe
good readability.

I would change the name of your macro. ASSERT what? IOW, what is it
that you're asserting? It's possible that what you want is a macro that
wraps your 'ASSERT' and the function call, something like

#define IF_ASSERT_CALL(f) ASSERT && f

and use it

IF_ASSERT_CALL(Do<Static_Data>());

(again, the word "ASSERT" seems out of place, perhaps you want to
replace it with something better describing the fact that it's a global
flag of sorts.

const bool MY_CODE_INSERTION_FLAG = false; // no need for 'static'
static const bool ASSERT = false;

struct Static_Data
{
static const char a = 1;
static const char b = 2;
static const char c = 3;
};

template< typename t_Static_Data>
bool Do()
{
printf( "Assert Message: %d %d %d\n", t_Static_Data::a,
t_Static_Data::b, t_Static_Data::c );
return true;
}

int main()
{
ASSERT&& Do< Static_Data>();

return 0;

}

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top