codeql-coding-standards icon indicating copy to clipboard operation
codeql-coding-standards copied to clipboard

`A7-1-2`: `constexpr` in templates

Open lcartey opened this issue 1 year ago • 0 comments

Affected rules

  • A7-1-2

Description

We should only report a variable in a template as a candidate for adding constexpr if all visible instantiations could use constexpr.

Example

int x;

constexpr int* init_t(int i) {
  return &x;
}

float* init_t(float f) {
  return new float();
}

template <typename T> void constexpr_variable_template() {
  constexpr T t1{};
  T* t = init_t(t1); // COMPLIANT[FALSE_POSITIVE] for int, this could be constexpr, but not for float
}

void test() {
  constexpr_variable_template<int>();
  constexpr_variable_template<float>();
}

lcartey avatar Jun 03 '24 15:06 lcartey