https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122945

            Bug ID: 122945
           Summary: Undefined reference to constructor of variant
                    alternative with both consteval and runtime
                    alternatives
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: driver
          Assignee: unassigned at gcc dot gnu.org
          Reporter: arseny.orlow at yandex dot ru
  Target Milestone: ---

Created attachment 62961
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=62961&action=edit
Example and linker output as separate files

Here is a minimal reproducible example:

file.cpp:
#include <variant>

struct A {
  int i;

  consteval A(int i) : i(i) {
  }
};

struct B {
  A a;
};

struct C {
  std::variant<A, B> v;

  C(std::variant<A, B> v) : v(v) {
  }
};

int main() {
  C c{42};
}

When compiled with
g++ --std=c++20 -Wall -Wextra -fno-strict-aliasing -fwrapv ./file.cpp
(tested with --std=c++23 and --std=c++26 too) it produces a linkage error:

/usr/bin/ld: /tmp/ccrJqkHJ.o: in function
`std::__detail::__variant::_Variadic_union<true, A,
B>::_Variadic_union<int>(std::in_place_index_t<0ul>, int&&)':
file.cpp:(.text._ZNSt8__detail9__variant15_Variadic_unionILb1EJ1A1BEEC2IJiEEESt16in_place_index_tILm0EEDpOT_[_ZNSt8__detail9__variant15_Variadic_unionILb1EJ1A1BEEC5IJiEEESt16in_place_index_tILm0EEDpOT_]+0x27):
undefined reference to `std::__detail::__variant::_Uninitialized<A,
true>::_Uninitialized<int>(std::in_place_index_t<0ul>, int&&)'
collect2: error: ld returned 1 exit status

This behavior occurs on all major versions from 14.1 to 15.2. All attempts were
performed on x86-64 machine.

Clang though successfully compiles and links this example, and execution of the
compiled binary finishes with exit code 0. 

Changing the constructor of A from consteval to constexpr or just removing it
makes the compilation successful.

Reply via email to