- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在使用一个封装了模板特化的 std::variant 的类,例如:
template<typename Type> struct Generic_node {...};
struct Leaf_node : Generic_node<...> {...};
struct Inner_node : Generic_node<...> {...};
struct Node {std::variant<Leaf_node, Inner_node> variant_;};
我正在尝试构建一个 Node
来自 Generic_node
中的函数使用转换 move 构造函数,但编译失败。
我定义了一个模板构造函数,它接受一个右值引用(假设是一个专门的类)并通过将值 move 到变体来构造变体,我希望在其中调用转换 move 构造函数 # (4) .
当我尝试创建最小的非工作示例时,我发现问题实际上只显示在模板函数中,如果我知道确切的类型(编译器知道的 Leaf_node == Generic_node<...>
), move 构造就会成功.因此,我假设一如既往地发生了一些我没有预料到的模板魔法。
#include <variant>
template<typename T>
struct Base
{
void f();
};
struct Derived : Base<int> {};
struct Variant
{
// In real program expecting the T to be one of the multiple variants
// Here I use only one variant because it suffices to illustrate the problem
template<typename T>
Variant(T&& t)
:
variant_ {std::move(t)}
{
}
std::variant<Derived> variant_;
};
template<typename T>
void
Base<T>::
f()
{
Variant {std::move(Derived {})}; // can call on exact type
Variant {std::move(Base<T> {})}; // can not call on specialized template type
}
int
main()
{
Derived {}.f();
}
相关编译错误信息(clang 7, libstdc++-8):
note: candidate template ignored: substitution failure [with _Tp = Base<int>,
$1 = void, $2 = void]: implicit instantiation of undefined template
'std::variant<Derived>::__to_type_impl<18446744073709551615, false>'
variant(_Tp&& __t)
问题很可能与变体无关,而是与 Base<T> == Derived
的相等性有关在 Variant 构造函数的模板实例化中,编译器好像没有看到它。
模板实例化中发生了什么,为什么编译器不能调用提供的构造函数?
编辑: 因为我打算创建一个专门化,所以我忘记了继承不能暗示类类型相等,即使在技术上是在这种特殊情况下。因此,通过从专门的 Base move 来构建 Derived 是一项简单的任务:
struct Derived : Base<int>
{
Derived() = default;
Derived(Base<int>&&) {}
};
如果我是正确的,则需要为 Base 的每个派生类显式定义构造函数。
最佳答案
在您给出的示例中,Derived 类是一个独立于 Base 的类。它们具有完全相同的成员、相同的方法,但它们仍然是独立的类。
解决它的最简单方法是使用 using
语句而不是将其声明为单独的类:
using Derived = Base<int>;
关于c++ - std::variant of template specializations 转换 move 构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51429511/
我正在尝试构建模板类 Fod template class Fod { ... }; 它将包含一个带有 static const int value 的内部类 At指示模板参数的索引(S0 为 0,S
我很难理解 Python in a Nutshell 的最后一部分(粗体) Per-Instance Methods An instance can have instance-specific bi
我需要密码字段的正则表达式。 要求是: 密码长度必须在8到20个字符之间 必须包含至少一个字母和一个数字以及来自!@#$%^&*() 的特殊字符_+。 不应以特殊字符开头 我试过了 ^(?=.*[a-
我有一个 C 类型的输入字段。 PARAMETERS lv_sep TYPE c. 字段 lv_sep 应该只接受特殊字符。 你能帮助我如何给出这个约束吗? 最佳答案 您可以在 AT-SELECTIO
我记得 PaulP 展示了一个很酷的技巧来缩写重复的长 @specialized序列,但我找不到原来的帖子了。就像我有 trait Foo[@specialized(Int, Float, Doubl
我有一个特征和一个实现,如下所示: trait Foo[A] { def bar[B >: A: Ordering]: Foo[B] } class FooImpl[A]( val a: A, v
在Sas9中,如何用下划线替换我选择的所有,\ /或空格以及其他特殊字符?无论是在数据步骤中还是在宏函数中的解决方案都可以解决问题,我只是在寻找一种实现方法。 谢谢 最佳答案 您可以使用SAS内置的P
SPECIALIZE 的目的pragma 是创建更具体的函数版本。 我有一个功能 adaptBlocks :: Int -> BlocksField a -> Maybe (BlocksField a
我尝试使用 gcc 10 -std=gnu++20 -fconcepts 构建以下内容: template class MyClass{ T a; }; template class MyClas
已关闭。这个问题是 off-topic 。目前不接受答案。 想要改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 已关闭11 年前。 Improve th
在哪里可以找到文件名中允许的字符列表(取决于操作系统)?(例如,在 Linux 上,文件名中允许使用字符 :,但在 Windows 上则不允许) 最佳答案 您应该从 Wikipedia Filenam
我有下面的powershell功能 Function Test { Param ( [Parameter()] [strin
我有下面的powershell功能 Function Test { Param ( [Parameter()] [strin
我有两个方阵 A 和 B。它们的每一项都有 1 或 0。示例如下所示 A channel id a b c 1 1 1 1 2 1 0 1 3 1 0 0 B id cha
是否可以将基础对象“特化”为派生对象? 例如: class base{... base(...) : ... {}//both have their own constructors virt
我有两个像这样连接在一起的圆圈: 我在形状内部有一个点,我想从该点沿一个方向将光线转换到形状上。为了检索形状边缘的类型转换位置。 我的第一个想法是将 2 段连接到 2 个圆圈进行光线转换。如果没有成功
在我的 Java 项目中,我有以下类/接口(interface)层次结构: public interface ProductSearcher { Set search(String reque
是否可以在不引入与实现完全匹配的签名的情况下定义这个专门的重载? on(eventName: string, cb: Function); on(eventName: "view", cb: (arg
偶然发现 def foo(f: Int => Unit) {} def foo(f: Long => Unit) {} 由于 method foo is defined twice 无法编译.我知道上
在下面的例子中,为什么 foo(f)叫暧昧? 我知道第二个重载也适用于 P == () , 但为什么第一个不被认为更专业, 因此更好的匹配? func foo(_ f: () -> R) { prin
我是一名优秀的程序员,十分优秀!