2

Why is the following not compiling? This is somehow counter-intuitive (not to say constexpr concepts are confusing):

#include <tuple>

int main() {
    constexpr const int a   = 0;
    static_assert(a == 0, "Wups");
    constexpr auto t2 = std::forward_as_tuple(a, a);
}

LIVE I assumed that a is a compile-time constant expression which is clearly the case as I can use it in static_assert.

Background: I wanted to continue with constexpr tuple of reference to feed to other constexpr functions to do some compile-time computations

Gabriel
  • 8,990
  • 6
  • 57
  • 101
  • 3
    See https://stackoverflow.com/questions/28614591/how-to-initialize-a-constexpr-reference, and in particular *"A constexpr reference has to bind to a global, not a local variable (or more formally, it has to bind to something with static storage duration)."*. – Holt Jan 02 '20 at 13:18
  • 1
    If you want to do compile-time calculations creating/passing by value is probably equivalent in many cases. – super Jan 02 '20 at 13:21
  • Are references necessary for your computations? – Gaurav Dhiman Jan 02 '20 at 13:22
  • no not really, I thought I can wrap already computed constexpr values into a tuple of reference. – Gabriel Jan 02 '20 at 13:36

0 Answers0