std :: variant在gcc 8和9中需要默认构造函数,而在gcc 10 / clang中则不需要

问题描述

我无法在Ubuntu 20.04.1 / gcc 9.3.0上编译以下代码。 根据{{​​3}},它可以使用gcc 10.x进行编译, 和gcc 7.x,但给出:

In file included from /usr/include/c++/9/variant:36,from test.cpp:1:
/usr/include/c++/9/type_traits: In instantiation of ‘struct std::__is_nt_default_constructible_atom<Foo>’:                                                                                                  
/usr/include/c++/9/type_traits:945:12:   required from ‘struct std::__is_nt_default_constructible_impl<Foo,false>’                                                                                         
/usr/include/c++/9/type_traits:131:12:   required from ‘struct std::__and_<std::is_default_constructible<Foo>,std::__is_nt_default_constructible_impl<Foo,false> >’                                       
/usr/include/c++/9/type_traits:951:12:   required from ‘struct std::is_nothrow_default_constructible<Foo>’                                                                                                  
/usr/include/c++/9/type_traits:2965:25:   required from ‘constexpr const bool std::is_nothrow_default_constructible_v<Foo>’                                                                                 
/usr/include/c++/9/variant:301:4:   [ skipping 2 instantiation contexts,use -ftemplate-backtrace-limit=0 to disable ]                                                                                      
/usr/include/c++/9/type_traits:883:12:   recursively required from ‘constexpr std::variant<_Types>::variant() [with _Types = {Foo,Boo}]’                                                                   
/usr/include/c++/9/type_traits:883:12:   required from ‘struct std::is_constructible<std::variant<Foo,Boo> >’                                                                                              
/usr/include/c++/9/type_traits:889:12:   required from ‘struct std::is_default_constructible<std::variant<Foo,Boo> >’                                                                                      
/usr/include/c++/9/type_traits:2921:25:   required from ‘constexpr const bool std::is_default_constructible_v<std::variant<Foo,Boo> >’                                                                     
/usr/include/c++/9/variant:273:4:   required from ‘constexpr const bool std::__detail::__variant::_Traits<std::variant<Foo,Boo>,std::__cxx11::basic_string<char,std::char_traits<char>,std::allocator<c\
har> > >::_S_default_ctor’                                                                                                                                                                                  
/usr/include/c++/9/variant:1219:11:   required from ‘class std::variant<std::variant<Foo,std::allocator<char> > >’                          
test.cpp:32:53:   required from here                                                                                                                                                                        
/usr/include/c++/9/type_traits:931:47: error: ‘Foo::Foo()’ is private within this context                                                                                                                   
  931 |     : public integral_constant<bool,noexcept(_Tp())>                                                                                                                                               
      |                                               ^~~~~                                                                                                                                                 
test.cpp:10:3: note: declared private here                                                                                                                                                                  
   10 |   Foo() noexcept {}                                                                                                                                                                                 
      |   ^~~                                      

使用gcc 8.x和9.x。波纹管有什么问题吗? 请注意,如果在下面的代码删除template <bool X>, 所有编译都很好。或者,我可以删除main中的代码,并且所有编译都可以。

#include <variant>
#include <string>

struct Foo {
public:
  explicit Foo(int) noexcept {}
  Foo(Foo &&) noexcept = default;
  Foo &operator=(Foo &&) = default;
private:
  Foo() noexcept {}
};

struct Boo {
public:
  explicit Boo(int) noexcept {}
  Boo(Boo &&) noexcept = default;
  Boo &operator=(Boo &&) = default;
private:
  Boo() noexcept {}
};


template<bool X>
std::variant<Foo,Boo> g(int v,int x) {
 return  v == 0 ? std::variant<Foo,Boo>{Foo{x}} :
                                 std::variant<Foo,Boo>{Boo{x}};
}


 int main()
{
  std::variant<std::variant<Foo,std::string> err{std::string("aaa")};
}

解决方法

根据标准,在这种情况下没有DefaultConstructible要求。

允许变体本身是Non-DefaultConstructible的(内部和外部变体在此示例中都是这种情况)。

如果Foo()私有构造函数被删除或删除,则code works(按预期):

struct Foo {
public:
  explicit Foo(int) noexcept {}
  Foo(Foo &&) noexcept = default;
  Foo &operator=(Foo &&) = default;
private:
//   Foo() noexcept {} -> comment out works
Foo() = deleted; // -> works also.
};

因此,这显然是编译器或标准库中的实现错误。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...