我正在努力学习Boost.Spirit,但我发现了一个难点.
我试图将字符串解析为以下结构:
struct employee {
std::string name;
std::string location;
};
Run Code Online (Sandbox Code Playgroud)
似乎当两个具有相同类型的属性背靠背时,它们会崩溃(逻辑上)std::vector为该类型的一个.由于该规则,以下解析器
+x3::ascii::alnum >>
+x3::space >>
+x3::ascii::alnum
Run Code Online (Sandbox Code Playgroud)
会有的属性std::vector<std::string>.
但我试图将其解析为struct,这意味着我的理想属性将是一个boost::fusion::tuple<std::string, std::string>,所以我可以调整我的结构.
不工作代码的完整版本(如上所述):
// Example program
#include <iostream>
#include <string>
#include <boost/spirit/home/x3.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
struct employee {
std::string name;
std::string location;
};
BOOST_FUSION_ADAPT_STRUCT(employee,
(std::string, name),
(std::string, location)
)
namespace x3 = boost::spirit::x3;
x3::rule<struct parse_emp_id, employee> const parse_emp = "Employee Parser";
auto parse_emp_def =
+x3::ascii::alnum >>
+x3::space >>
+x3::ascii::alnum
;
BOOST_SPIRIT_DEFINE(parse_emp);
int main()
{
std::string input …Run Code Online (Sandbox Code Playgroud) 我无法想象为什么选择std::bitset::size非静态的.这使得获得constexpr尺寸变得更加困难; 你必须写这样的东西:
template<int val>
struct int_
{
static const constexpr value = val;
};
template<size_t size>
auto getBitsetSizeIMPL(std::bitset<size>)
{
return int_<size>{};
}
template<typename BitsetType>
constexpr size_t getBitsetSize()
{
return decltype(getBitsetSizeIMPL(BitsetType{}))::value;
}
Run Code Online (Sandbox Code Playgroud)
如果它是静态的,你所要做的就是
BitsetType::size()
Run Code Online (Sandbox Code Playgroud)
并且不会牺牲功能.
是否有一个历史原因,我失踪或有一个我失踪的技术事实?
今天,我偶然发现了以下代码片段:
#include <utility>
int main()
{
auto a = [](std::pair<auto, auto> value)
{
};
a(std::pair<int, bool>{ 3, true });
}
Run Code Online (Sandbox Code Playgroud)
我只有一个问题:标准是否支持此代码?
它在GCC(with -std=c++14)中编译,但不是clang或Visual Studio 2015(VC++ 14).
这似乎应该是标准的一部分,因为如果lambdas应该具有与常规函数相同的模板支持,那么应该支持它.
这似乎转换为所有模板类型,而不仅仅是std::pair.
我正在尝试进行一些基础char16_t字符串(u16string)处理,并且遇到了一些麻烦。这个简短的程序:
#include <string>
#include <sstream>
int main()
{
int foo = 65;
std::basic_stringstream<char16_t> ss;
ss << foo;
std::u16string s = ss.str();
}
Run Code Online (Sandbox Code Playgroud)
产生错误:
Error C2491 'std::numpunct<_Elem>::id': definition of dllimport static data member not allowed. xlocnum 259
Run Code Online (Sandbox Code Playgroud)
我已经在一些在线编译器上尝试过了,但是那里没有错误。
感谢您的任何帮助!
以下代码在 g++ 9 下编译,带有标志-std=c++17,但不是带有相同标志的 clang 8:
#include <string_view>
#include <cstdlib>
int main() {
constexpr std::string_view hello = "hello";
constexpr size_t last_l = hello.find_last_of("lo");
}
Run Code Online (Sandbox Code Playgroud)
错误信息如下:
test.cpp:8:19: error: constexpr variable 'last_l' must be initialized by a constant expression
constexpr size_t last_l = hello.find_last_of("lo");
^ ~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/char_traits.h:349:9: note: cast from 'void *' is not allowed in a constant expression
return static_cast<const char_type*>(__builtin_memchr(__s, __a, __n));
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/string_view.tcc:150:12: note: in call to 'find(&"lo"[0], 2, "hello"[4])'
if (traits_type::find(__str, __n, this->_M_str[__size]))
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/string_view:402:22: note: …Run Code Online (Sandbox Code Playgroud) 我正在尝试从 Ubuntu 20.04 映像使用 Ubuntu 22.04 的 sysroot 进行编译,但遇到了很多麻烦,出现了如下链接器错误:
/usr/bin/ld: ../build_rootfs/x86_64_jammy/lib/x86_64-linux-gnu/libc.so.6: undefined reference to `_dl_audit_symbind_alt@GLIBC_PRIVATE'
/usr/bin/ld: ../build_rootfs/x86_64_jammy/lib/x86_64-linux-gnu/libc.so.6: undefined reference to `_dl_audit_preinit@GLIBC_PRIVATE'
/usr/bin/ld: ../build_rootfs/x86_64_jammy/lib/x86_64-linux-gnu/libc.so.6: undefined reference to `_dl_fatal_printf@GLIBC_PRIVATE'
/usr/bin/ld: ../build_rootfs/x86_64_jammy/lib/x86_64-linux-gnu/libc.so.6: undefined reference to `__nptl_change_stack_perm@GLIBC_PRIVATE'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Run Code Online (Sandbox Code Playgroud)
完全相同的调用在 Ubuntu 22.04 上运行良好,使用完全相同的编译器和完全相同的 sysroot。
这是我的设置:
/usr/bin/ld: ../build_rootfs/x86_64_jammy/lib/x86_64-linux-gnu/libc.so.6: undefined reference to `_dl_audit_symbind_alt@GLIBC_PRIVATE'
/usr/bin/ld: ../build_rootfs/x86_64_jammy/lib/x86_64-linux-gnu/libc.so.6: undefined reference to `_dl_audit_preinit@GLIBC_PRIVATE'
/usr/bin/ld: ../build_rootfs/x86_64_jammy/lib/x86_64-linux-gnu/libc.so.6: undefined reference to `_dl_fatal_printf@GLIBC_PRIVATE'
/usr/bin/ld: ../build_rootfs/x86_64_jammy/lib/x86_64-linux-gnu/libc.so.6: undefined reference to `__nptl_change_stack_perm@GLIBC_PRIVATE'
clang: error: linker …Run Code Online (Sandbox Code Playgroud) 有没有办法使用存储在变量中的名称(用于将函数传递给函数等)在CMake中调用函数?
这是我尝试过的:
cmake_minimum_required(VERSION 3.0)
function(doThing)
endfunction()
set(FuncVar doThing)
${FuncVar}()
Run Code Online (Sandbox Code Playgroud)
哪个失败了这个错误:
Parse error. Expected a command name, got unquoted argument with text "${FuncVar}".
-- Configuring incomplete, errors occurred!
Run Code Online (Sandbox Code Playgroud)
我不明白为什么这不起作用,但我又是新来的CMake所以我知道什么.
感谢您的任何帮助!
今天我学习了完美的转发,我创建了这个代码示例
#include <utility>
#include <functional>
template<typename Function, typename... Args>
auto toStdFun(Function&& fun, Args&&...args)
{
using retType = decltype(fun(std::forward<Args>(args)...));
return std::function<retType(decltype(std::forward<Args>(args))...)>(fun);
}
int main()
{
toStdFun([] () {});
}
Run Code Online (Sandbox Code Playgroud)
然后,有人告诉我使用decltype(std::forward<Args>(args))...可以简单地表示Args&&...如下:
#include <utility>
#include <functional>
template<typename Function, typename... Args>
auto toStdFun(Function&& fun, Args&&...args)
{
using retType = decltype(fun(std::forward<Args>(args)...));
return std::function<retType(Args&&...)>(fun);
}
int main()
{
toStdFun([] () {});
}
Run Code Online (Sandbox Code Playgroud)
两个样本有什么区别?
所以我正在尝试使用boost::hana一个需要使用功能来基于值获取元素索引的库:
constexpr auto tup = boost::hana::make_tuple(3_c, boost::hana::type_c<bool>);
auto index = get_index_of_first_matching(tup, boost::hana::type_c<bool>);
// ^^^^^ would be a boost::hana::int_<1>
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点?更好的是,它已经存在了hana,我不知道吗?
感谢您的支持!
前几天我正在乱搞lambdas和std :: functions,发现了一个奇怪的属性.捕获的变量超出范围后,它们仍然有效.
这是一个例子来说明我的意思.
#include <iostream>
#include <functional>
std::function<void()> make_lambda()
{
int a = 10;
return [&a](){std::cout << a << std::endl;};
}
int main()
{
auto fun = make_lambda();
fun();
}
Run Code Online (Sandbox Code Playgroud)
对我来说,这似乎不起作用,a通过引用捕获,并且引用已经消失.
编辑:
好.这个问题不仅与lambada有关,而是在使用前删除了所有引用.
我的问题已经改变了.我仍然使用大量的堆栈来获得类似的行为,这必须覆盖旧数据.这是我的新代码:
#include <iostream>
int& make()
{
int a = 10;
return a;
}
void flushStack(long long i)
{
if (i == 0)
{
return;
}
flushStack(i-1);
}
int main()
{
int& i = make();
std::cout << i++ << '\n';
std::cout << i++ << '\n'; …Run Code Online (Sandbox Code Playgroud) c++ ×9
c++11 ×5
boost ×2
c++14 ×2
clang ×2
lambda ×2
bitset ×1
boost-hana ×1
boost-spirit ×1
c ×1
c++17 ×1
cmake ×1
constexpr ×1
g++ ×1
glibc ×1
linux ×1
std ×1
std-function ×1
templates ×1
visual-c++ ×1