小编Rus*_*ene的帖子

Boost.Spirit.x3避免将相同类型的两个连续属性折叠到向量中

我正在努力学习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)

c++ boost boost-spirit c++14 boost-spirit-x3

9
推荐指数
1
解决办法
311
查看次数

为什么std :: bitset :: size是非静态的

我无法想象为什么选择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)

并且不会牺牲功能.

是否有一个历史原因,我失踪或有一个我失踪的技术事实?

c++ std bitset c++11

7
推荐指数
1
解决办法
410
查看次数

lambdas中的auto模板参数列表是标准的一部分吗?

今天,我偶然发现了以下代码片段:

#include <utility>

int main()
{

  auto a = [](std::pair<auto, auto> value)
  {

  };

  a(std::pair<int, bool>{ 3, true });
}
Run Code Online (Sandbox Code Playgroud)

http://cpp.sh/5p34

我只有一个问题:标准是否支持此代码?

它在GCC(with -std=c++14)中编译,但不是clang或Visual Studio 2015(VC++ 14).

这似乎应该是标准的一部分,因为如果lambdas应该具有与常规函数相同的模板支持,那么应该支持它.

这似乎转换为所有模板类型,而不仅仅是std::pair.

c++ lambda templates c++11 type-deduction

6
推荐指数
1
解决办法
750
查看次数

在VC ++ 14中使用std :: basic_stringstream &lt;char16_t&gt;时出错

我正在尝试进行一些基础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)

我已经在一些在线编译器上尝试过了,但是那里没有错误。

感谢您的任何帮助!

c++ visual-c++ visual-studio-2015

5
推荐指数
1
解决办法
609
查看次数

constexpr std::string_view::find_last_of 在带有 libstdc++ 9 的 clang 8 上不起作用

以下代码在 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)

c++ g++ clang constexpr c++17

5
推荐指数
0
解决办法
415
查看次数

使用较新的 glibc 将 Clang 编译到 sysroot 中

我正在尝试从 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)

c c++ linux glibc clang

5
推荐指数
1
解决办法
832
查看次数

如何在名称存储在变量中的CMake中调用函数

有没有办法使用存储在变量中的名称(用于将函数传递给函数等)在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所以我知道什么.

感谢您的任何帮助!

cmake

4
推荐指数
3
解决办法
2639
查看次数

完美转发`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(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)

两个样本有什么区别?

c++ perfect-forwarding c++11 std-function c++14

3
推荐指数
1
解决办法
289
查看次数

Boost Hana获取第一个匹配项的索引

所以我正在尝试使用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,我不知道吗?

感谢您的支持!

c++ boost template-meta-programming c++11 boost-hana

3
推荐指数
1
解决办法
605
查看次数

为什么从lambdas创建的std :: functions在捕获的变量超出范围后工作?

前几天我正在乱搞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++ lambda function-pointers c++11

0
推荐指数
1
解决办法
94
查看次数