Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

Variant fails with private default constructor

Đang mở
#103 0 bình luận 1 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
45/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
cpp
Lĩnh vực
backend

Hướng nghiên cứu

Bắt đầu với ví dụ tối thiểu và phiên bản trình biên dịch trong issue, sau đó kiểm tra boost/variant/variant.hpp xung quanh find_fallback_type và boost/type_traits/has_nothrow_constructor.hpp. Xác nhận lỗi và so sánh hành vi với Boost.Variant2; được xem là hoàn tất khi ví dụ biên dịch được với một hàm khởi tạo mặc định private trên các phiên bản GCC bị ảnh hưởng mà không phá vỡ hành vi fallback hiện có.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Boost.Variant fails if its types have a private default constructor, using gcc 12.2
Minimal example: https://godbolt.org/z/cG4PnW8ee

#include <boost/variant.hpp>
#include <boost/variant2.hpp>
class Foo
{
    public:
        Foo(int a_) : a(a_) {}
        int a;
    private:
        Foo();
};
struct Voo
{
    Foo f;
    double d;
};
struct Doo
{
    Foo f;
    int s;
};
#ifndef USE_VARIANT2
    using VooDoo = boost::variant<Voo, Doo>;
    void testVooDoo(const VooDoo& voodoo)
    {
        VooDoo v{Voo{Foo(3)}};
    } 
#else
    using VooDoo = boost::variant2::variant<Voo, Doo>;
    void testVooDoo(const VooDoo& voodoo)
    {
        VooDoo v{Voo{Foo(3)}};
    }
#endif

If I change to use Boost.Variant2, it works.
If I go back to gcc 7.5, it works (fails after that version).

The error shows:

/opt/compiler-explorer/libs/boost_1_80_0/boost/variant/variant.hpp:194:17:   required from 'struct boost::detail::variant::find_fallback_type<boost::mpl::l_item<mpl_::long_<2>, Voo, boost::mpl::l_item<mpl_::long_<1>, Doo, boost::mpl::l_end> > >'
/opt/compiler-explorer/libs/boost_1_80_0/boost/variant/variant.hpp:1252:17:   required from 'class boost::variant<Voo, Doo>'
<source>:26:16:   required from here
/opt/compiler-explorer/libs/boost_1_80_0/boost/type_traits/has_nothrow_constructor.hpp:27:84: error: 'Foo::Foo()' is private within this context
   27 | template <class T> struct has_nothrow_constructor : public integral_constant<bool, BOOST_HAS_NOTHROW_CONSTRUCTOR(T)>{};
      |                                                                                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<source>:9:9: note: declared private here
    9 |         Foo();
      |         ^~~

It mentions find_fallback_type, so I guess that at some point it tries to determine the "fallback type" to default construct the VooDoo.
However, nowhere in my program I need to default construct a VooDoo, so I'm not sure why that type needs to be "computed".

Also, somehow Boost.Variant2 doesn't fail, so there must be a way to make this work.

Ngôn ngữ chính
C++
Star
47
Fork
72
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của boostorg/variant

Tất cả issue của boostorg/variant

Issue tương tự

Thêm issue về C++

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.