Home > Mobile >  what's the meaning of the include sentence between the angle brackets?
what's the meaning of the include sentence between the angle brackets?

Time:01-19

Recently I read a block of code like that:

void ToyDialect::initialize() {
  addOperations<
#define GET_OP_LIST
#include "toy/Ops.cpp.inc"
      >();
}

It seems that the addOperation is a template function. But I don't know why there is a non-sense define and a include between the angle brackets. And there're also irregular indent ahead of these two sentence. Anyone can tell me the effect of this kind of code?

CodePudding user response:

toy/Ops.cpp.inc likely includes a comma-separated list of types that are used as arguments to the type template parameter pack of the addOperations (function?) template.

// in toy/Ops.cpp.inc
Type1,
Type2,
Type3

meaning this

void ToyDialect::initialize() {
  addOperations<
#define GET_OP_LIST
#include "toy/Ops.cpp.inc"
      >();
}

expands, after pre-processing step, to:

void ToyDialect::initialize() {
  addOperations<
#define GET_OP_LIST
    Type1,
    Type2,
    Type3
      >();
}

The placement of the #define directive could be suspected to affect the active content of the toy/Ops.cpp.inc file (the latter may in turn use pre-processor directives, e.g. #ifdef and so on).

CodePudding user response:

what's the meaning of the include sentence between the angle brackets?

#define GET_OP_LIST

This is a pre-processor macro definition directive. The effect is that the pre-processor will remove it from the source, but the macro definition may affect the behaviour of the pre-processor if other directives use that macro.

#include "toy/Ops.cpp.inc"

This is a pre-processor include diretive. The effect is that the pre-processor replaces the directive with the content of the file named by the argument.

Pre-processor directives are agnostic of the context where they are in, except for other pre-processor directives. Hence, the meaning of these directives remains the same whether they are between the angle brackets or not. Of course, since the include directive is between the angle brackets, the code included from the file will be included between the angle brackets.

And there're also irregular indent ahead of these two sentence.

All indentations are equal in the C language, and have no effect on its meaning.


In a call to a function template, the angle brackets denote the template arguments used to instantiate the template. So, to conclude in human words: The meaning of the quoted code is to include the template arguments from another file.

  •  Tags:  
  • Related