- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
以下代码无法编译:
#include <boost/variant.hpp>
class A {};
class B {};
class C {};
class D {};
using v1 = boost::variant<A, B>;
using v2 = boost::variant<C, D>;
int f(v1 const&) {
return 0;
}
int f(v2 const&) {
return 1;
}
int main() {
return f(A{});
}
gcc 和 clang 都提示:
test1.cpp: In function ‘int main()’:
test1.cpp:18:17: error: call of overloaded ‘f(A)’ is ambiguous
return f(A{});
^
test1.cpp:18:17: note: candidates are:
test1.cpp:11:5: note: int f(const v1&)
int f(v1 const&) {
^
test1.cpp:14:5: note: int f(const v2&)
int f(v2 const&) {
既然不可能从 A
实例构造一个 v2
对象,为什么编译器在重载决策期间给予两个函数相同的优先级?
最佳答案
问题出在构造函数上
template<typename T>
variant(const T&)
boost::variant
。下面的代码在没有任何魔法的情况下重现了这个问题:
struct C {};
struct A {
A(const C&) {}
template<typename T>
A(const T&) {}
};
struct B {
template<typename T>
B(const T&) {}
};
int f(const A&) {
return 0;
}
int f(const B&) {
return 1;
}
int main() {
return f(C{});
}
我认为 variant
构造函数只有在argument 实际上可以转换为 arguments,你可能想要将其作为错误提出。
关于c++ - boost::variant 和函数重载决议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24791319/
我创建了一个简单的 Variant 类来存储字符串、整数、 double 等。我正在尝试使用 std::map 类型的映射但我收到了这个奇怪的错误: In file included from /us
我有一个整数数组来检索 std::variant 中的内容。但是编译失败,报错No matching function to call 'get' .您能否解释原因,并提出实现相同目的的可行解决方案?
我的问题涉及 boost::variant 转换与 C++ 中的 std::vector 的混合。在我的项目中,我使用变体作为 SQL 的输出。我将始终只使用一种类型的变体。我想要做的是从变量、变量
(警告:虽然它乍一看可能是一个问题,但这是 而不是 一个初学者级别的问题。如果您熟悉“让强制”这个短语或者您曾经查看过 VBA 规范,请继续阅读。) 假设我有一个 Variant 类型的表达式,我想将
我正在使用一个相当笨拙的 c 接口(interface)来存储集合。 LowLevelStorer 类表示我为此接口(interface)编写的包装器。 Storer 类是一个高级类,它与Data 有
我试图将两个变体组合成一个变体只是为了便于阅读。这是代码: using VariantType_basic = std::variant; using VariantType_vector = std
给定一个类型为 std::variant 的变量,我检查过它不包含C .如何将其转换为 std::variant ? std::variant convert(std::variant value)
使用boost:variant: #include #include #include template boost::variant _tuple_index(size_t i, const
在 answer 中对于这个 SO 问题: What is the equivalent of boost::variant in the C++ standard library? 提到boost:
自从在 Android Gradle 插件 0.13.0 中升级到 gradle 2.1 后,这个问题就出现了,但我一直无法理解为什么有时会记录此警告。 考虑此 block 以根据变体类型重命名 AP
我想为变量实现一个模板方法。 但是根据输入是否为 int 变量(char、short、int),我想处理它与输入为 float 变量(float、double、long double)的情况有所不同。
boost::variant通过 boost::variant<>::types 公开其变体类型列表, 可以方便地与 boost::mpl::for_each 一起使用. std::variant缺少
歌词: 我尝试通过 MPI 实现任务池。所以我需要某种 RPC,但它可以在我的程序的不同部分之间工作,这意味着处理器 A 希望处理器 B 以参数 D 调用函数 C。我们不能像处理线程那样在进程之间传递
目前我的库使用 boost::optional 和 boost::variant。由于 C++17 已经发布,我想添加一个选项,它可以与 boost 和 std 一起使用。 所以我成功地测试了带有 b
假设我有: class TypeA { }; class TypeB { }; typedef boost::variant Type; 没关系: void foo(Type t) { }; int
假设我有一个嵌套的 boost::variant -类型TNested包含一些类型和一些其他 boost::variant类型(它本身不能再次包含 boost::variant types ,因此不会
使用带有 gradle 插件版本 3.3.0-alpha11 的 Android Studio 3.3 Canary 11。尝试同步 gradle 时会抛出以下错误 WARNING: API 'var
我看过这篇关于使用 std::variant 的文章.这是因为以下代码引发了代码分析警告: void CChristianLifeMinistryHtmlView::OnTimer(UINT_PTR
我有一个 Delphi 6 类对象,其中包含 30 个变体的数组,每个变体都通过不同的索引属性公开。例如: property responseCode: integer Index 7
...或在内部快速更改类型 std::variant在源代码中。 下面是列表本身及其容器元素的头文件中的代码。 // HVector.hh class HVector: public std::vec
我是一名优秀的程序员,十分优秀!