13
#include <iostream>

struct Index {
    constexpr operator int() const { return 666; }
};

template <int i> void foo() {
    std::cout << i << std::endl;
}

void wrapper(Index index) {
    foo<index>();
}

int main() {
    Index index;
//    foo<index>(); // error: the value of ‘index’ is not usable in a constant expression
    wrapper(index);
}

Hello, everyone.

I'm using a constexpr conversion of a variable "index" to an int value, which is substituted to a "foo" templated function. If I directly call foo<index>() from "main", I get a compiler error. If the same call is done from the "wrapper", then everything compiles and works fine.

What am I missing there?

Compile command: g++ -std=c++14 main.tex with g++ (GCC) 5.3.1 20160406 (Red Hat 5.3.1-6).

Sviatoslav V.
  • 671
  • 6
  • 18
Dr. Zoidberg
  • 155
  • 5

1 Answers1

0

A very similar error was reported here. It was first reported with GCC 4.9.0

In the analysis provided:

This is a GCC bug. It appears to be some sort of confusion in the way GCC handles internal linkage non-type template parameters of pointer type.

It has since been resolved.

P.W
  • 26,289
  • 6
  • 39
  • 76