0

headerfile.h

#define NBIT 128
#define DATASIZE 34

#define STR_HELPER(x) #x
#define STR(x) STR_HELPER(x)


template <unsigned int POWER>
class xzcMutatorECB128
{

#define POWERLIMINT (NBIT * DATASIZE * 0.7)
#define MSG2 "xzcMutatorECB128<POWER>; error template parametr: POWER; maximum="STR(POWERLIMINT)"; [en]: Key exceeded the limit;"
    static_assert(!(POWER > POWERLIMINT), MSG2);

public:
  xzcMutatorECB128();
}

main.cpp

#include "headerfile.h"
int main(int argc, char *argv[])
{
xzcMutatorECB128<3400> xzc128e;
}

result:

error: C2338: xzcMutatorECB128<POWER>; error template parametr: POWER; maximum=(128 * 34 * 0.7); [en]: Key exceeded the limit;

how to do so:

error: C2338: xzcMutatorECB128<POWER>; error template parametr: POWER; maximum=3046; [en]: Key exceeded the limit;

NBIT and DATASIZE will change

zizix
  • 369
  • 1
  • 2
  • 5
  • There are macros that can do Math, but nothing this complicated. – chris Jun 22 '16 at 00:40
  • The preprocessor is a simpleton that knows nothing about C++. – Captain Obvlious Jun 22 '16 at 00:41
  • You comment on Darkrift's deleted answer *"NBIT and DATASIZE will change"* - such details should be in your question if you want a usable answer. Anyway, redefining them after the template's been parsed won't affect the template instantiations. ***You should probably be using normal variables or template parameters and not preprocessor macros.*** If you use macros to get the powerlimit value into the static assertion message, try making it a template parameter with default value `(NBIT * DATASIZE * 0.7)` - your compiler may show you the value in the assertion stack information. – Tony Delroy Jun 22 '16 at 01:54
  • See also: http://stackoverflow.com/a/7779566/410767 – Tony Delroy Jun 22 '16 at 01:55

2 Answers2

0

This is not possible in standard C++.

Preprocessor directives, like

#define

and the

    #x

stringifying operator -- they get executed in the preprocessing phase of compilation. The "#" stringifying operator gets executed, and replaces its parameter with the stringified version during the preprocessing phase.

Compilation and optimization does not occur until a later, compilation phase. By the time the compiler looks at the generated code, the parameter is already stringified.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
0

The definition of the template class xzcMutatorECB128 is wrong. If your want to define a template class, you may not know the data type of it in the compiling time. On the other hand, the data or function in the class should be either public or private or protected.

Lindsay. W
  • 36
  • 2