void foo() try {} catch (...) {}
// OK, function-try-block
Run Code Online (Sandbox Code Playgroud)
[]() try {} catch (...) {} ();
// error: expected ‘{’ before ‘try’
Run Code Online (Sandbox Code Playgroud)
[]() { try {} catch (...) {} } ();
// OK, extra curly braces`
Run Code Online (Sandbox Code Playgroud)
为什么不允许第二种变体?
我想boost::variant用C++ 17 替换s std::variant并摆脱boost::recursive_wrapper,在下面的代码中完全消除对boost的依赖.我该怎么办?
#include <boost/variant.hpp>
#include <type_traits>
using v = boost::variant<int, boost::recursive_wrapper<struct s> >;
struct s
{
v val;
};
template<template <typename...> class R, typename T, typename ... Ts>
auto reduce(T t, Ts ... /*ts*/)
{
return R<T, Ts...>{t};
}
template<typename T, typename F>
T adapt(F f)
{
static_assert(std::is_convertible_v<F, T>, "");
return f;
}
int main()
{
int val1 = 42;
s val2;
auto val3 = adapt<v>(reduce<boost::variant>(val1, val2));
}
Run Code Online (Sandbox Code Playgroud)
有两个通用函数:第一个函数reduce在运行时选择返回哪个参数(这里它只返回第一个参数以便简洁),第二个函数adapt将类型F的值转换为类型T的值. …
我想export(C ++ 20)别名模板。VC ++ 2019编译代码。Clang报告错误。哪一个是正确的,为什么?
// file: m.cppm
export module m;
template<typename T> struct my_template {};
export template<typename T> using my_alias = my_template<T>;
Run Code Online (Sandbox Code Playgroud)
// file: main.cpp
import m;
int main() { my_alias<int> v; }
Run Code Online (Sandbox Code Playgroud)
main.cpp:2:28: error: definition of 'my_template' must be imported from module 'm' before it is required
int main() { my_alias<int> v; }
^
m.cppm:3:29: note: previous definition is here
template<typename T> struct my_template {};
Run Code Online (Sandbox Code Playgroud) 为什么这不编译?(评论r3将编译,但我希望在规则中使用分号)
#include <boost/spirit/home/x3.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <string>
#include <vector>
struct v
{
std::string value;
};
BOOST_FUSION_ADAPT_STRUCT
(
v,
(std::string, value)
)
using namespace boost::spirit::x3;
auto r1 = rule<struct s1, std::string>{} = lexeme[+alpha];
auto r2 = rule<struct s2, v>{} = r1;
using ast = std::vector<v>;
auto r3 = rule<struct s3, ast>{} = *(r2 >> ';');
//auto r3 = rule<struct s3, ast>{} = *r2;
int main()
{
std::string script("a;");
auto begin = script.begin();
auto end = script.end();
ast a;
phrase_parse(begin, …Run Code Online (Sandbox Code Playgroud)