- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有 4 个函数:
boost::variant<type1,type2,type3>processFile(const char* file){
if(----expression that if true means I have to process as type 1----)
return processType1(file); //this just returns type1
else if(----expression that if true means I have to process as either type 1 or type 2----)
return processType23(file); //this returns boost::variant<type2,type3>. This also calls 2 more processing functions depending on type.
}
processType23 接受一个脚本文件,该文件将确定返回哪种类型。我想在这个文件中保留类型的确定。但是我不能返回 boost::variant。我收到以下错误:
error: could not convert 'engine::fileManager::processLua(const char*)()' from 'boost::variant<engine::material, engine::shader>' to 'boost::variant<engine::material, engine::shader, unsigned int>'
返回数据的(正确)方式是什么?
最佳答案
你应该使用访问者:
template <typename R, typename A> convert_variant(A const& arg) {
return boost::apply_visitor([](auto const& v) -> R { return R{v}; }, arg);
}
针对@llonesmiz 的观察,您可能希望编译转换代码,即使某些转换可能是非法的。在那种情况下,您将不得不使用一些类型特征来区分这些情况并采取相应的行动:
#include <boost/variant.hpp>
#include <boost/type_traits.hpp>
#include <iostream>
template <typename R>
struct convert_variant_visitor : boost::static_visitor<R> {
struct bad_conversion : std::runtime_error {
bad_conversion(std::string msg) : std::runtime_error(msg) {}
};
template <typename T>
typename boost::enable_if_c<boost::is_convertible<T, R>::value, R>::type
operator()(T const& v) const
{
return R(v); // or just v
}
template <typename T>
typename boost::enable_if_c<not boost::is_convertible<T, R>::value, R>::type
operator()(T const& v) const
{
throw bad_conversion(std::string("Cannot convert ") + typeid(v).name() + " to " + typeid(R).name());
//throw bad_conversion("Not convertible to variant");
}
};
template <typename R, typename A> R convert_variant(A const& arg) {
return boost::apply_visitor(convert_variant_visitor<R>(), arg);
}
int main() {
typedef boost::variant<std::string, int, double> V1;
typedef boost::variant<int, double> V2;
V1 input = 42;
V2 output = convert_variant<V2>(input);
std::cout << "input: " << input << " (which: " << input.which() << ")\n";
std::cout << "output: " << output << " (which: " << output.which() << ")\n";
}
打印
input: 42 (which: 1)
output: 42 (which: 0)
现代 C++ 功能正在使这样的通用代码变得更加简单:
template <typename R, typename A> R convert_variant(A const& arg) {
return boost::apply_visitor([](auto const& v) -> R {
if constexpr (std::is_convertible_v<decltype(v), R>)
return v;
else
throw std::runtime_error("bad conversion");
} , arg);
}
关于c++ - 如何返回由 boost::varaint 返回类型中包含的类型的子集构成的 boost::variant,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47195247/
您好,我正在使用 AWS EKS 开发 kubernetes。当我将 docker-compose 文件转换为 kompose 文件时,我遇到了 kompose 文件的问题,我遇到了卷挂载点问题,而且
在将密码转换为二进制哈希值以存储在数据库中时,我注意到除了通常的乱码之外,还有一些引号、空格和字母表,这些巧合可能构成有效 SQL 语句的一部分。 出于好奇,我想知道是否有人遇到过任何字符串在哈希后神
我的组件具有动态部分和 compose。动态部分在其他模块中,即节点项目。 如果我想在页面中使用自定义元素,例如: 我收到一条错误消息,指出无法在 ./my-custom-element/someV
我有一个 pandas 数据框,其中一列由数组组成。所以每个单元格都是一个数组。 假设数据框 df 中有一个列 A,这样 A = [ [1, 2, 3], [4, 5, 6],
当 HTTP 请求和响应在互联网上传输时,请求中文本的格式是什么?是 ASCII 码吗? 例子:如果 HTTP 请求如下所示 - GET /mysite/ HTTP/1.1 -- rest of t
我是一名优秀的程序员,十分优秀!