llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

non-constexpr recursive variable templates are instanciated wrongly

Open fmorgner opened this issue 10 years ago • 3 comments

Bugzilla Link 29033
Resolution DUPLICATE
Resolved on Mar 09, 2021 10:10
Version 3.8
OS Linux
Attachments Code generated by clang++ -std=c++14 -Wall -Wextra -pedantic -S
CC @zygoloid

Extended Description

The following code:

#include

template<std::size_t N> auto fibo_v{fibo_v + fibo_v};

template<> auto fibo_v<0>{0};

template<> auto fibo_v<1>{1};

int main() { std::cout << fibo_v<4> << '\n'; }

produces 0 as its output. Adding constexpr to the variable declarations resolves this and produces the correct result 3.

As far as i can tell, N3651 lifted the restriction on variable templates to be constexpr, thus i would expect the same result with and without constexpr in the declarations.

GCC 6.1.1 produces the correct output, regardless of constexpr being in place or not.

fmorgner avatar Aug 18 '16 10:08 fmorgner

mentioned in issue llvm/llvm-bugzilla-archive#49458

mentioned in issue llvm/llvm-bugzilla-archive#49458

miyuki avatar Nov 27 '21 04:11 miyuki

Not a bug: implicit instantiations of a variable template have unordered initialization. There is no guarantee that fibo_v<3> and fibo_v<2> are initialized before fibo_v<4>.

*** This bug has been marked as a duplicate of bug llvm/llvm-bugzilla-archive#49458 ***