You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GCC中条件显式构造器继承Bug及替代方案问询

问题:继承std::expected子类时GCC的显式构造器继承Bug

我们的场景是创建std::expected的子类,以下代码可复现相关异常行为:

#include <type_traits>

template<class T>
struct Foo{
  T value;

  constexpr explicit operator bool(){return true;}
  
  constexpr Foo() {}
  
  template<class U=T>
  constexpr explicit(!std::is_convertible_to<U,T>) 
  Foo(U&& v) : value(std::forward<U>(v)) {}
};

template <class T>
struct Bar : public Foo<T>  {
  using Foo<T>::Foo;
};

Bar继承了Foo的所有构造器,包括构造器的显式性。但在GCC 12、13版本中出现异常:

// Foo<bool> foo() {return Foo<int>();} // 符合预期,无法编译
Bar<bool> bar(){ return Bar<int>();}
// 这段代码居然可以编译通过!

Clang中无此问题,Bar的行为与Foo一致,上述代码无法编译。

显式为Bar添加构造器可以解决问题,但std::expected拥有大量复杂构造器,这种方案完全不可行。

经确认这是GCC的已知Bug(对应Bug编号:96848、85251),涉及C++标准条款[namespace.udecl] p13。现寻求无需复制<expected>代码的可行替代方案。

内容的提问来源于stack exchange,提问作者shirleyquirk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 09:12:32