小编Tak*_*ndo的帖子

类模板非依赖成员变量中是否需要模板限定符?

当我在注释行上编译以下代码时,我得到编译错误"错误:使用'template'关键字将'foo'视为依赖模板名称".(TEST4)

代码的所有其他部分都已成功编译.

#include <tuple>

struct my {
    template <typename T>
    void foo() {}
};

void test1() {
    my m;
    auto tr = std::forward_as_tuple(m);
    auto& t0 = std::get<0>(tr);
    t0.foo<int>();
}

template <typename T>
struct test2 {
    void method() {
        my m;
        auto tr = std::forward_as_tuple(m);
        auto& t0 = std::get<0>(tr);
        t0.foo<int>();
    }
};

template <typename T>
struct test3 {
    void method() {
        m.foo<int>();
    }
    my m;
};

template <typename T>
struct test4 {
    void method() {
        auto tr = std::forward_as_tuple(m);
        auto& t0 …
Run Code Online (Sandbox Code Playgroud)

c++ gcc templates clang

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

如何使用gdb查看boost :: multi_index散列索引的数据

我想看看boost :: multi_index(版本1.67.0)使用gdb包含的数据.首先我尝试了https://github.com/ruediger/Boost-Pretty-Printer.似乎hashed_unique不支持散列索引.

我注意到,如果第一个索引是支持的类型,如sequencedBoost-Pretty-Printer工作正常.但是,我现在无法编辑代码.我需要调试核心文件和二进制可执行文件.

我试图理解带有散列索引的multi_index的内部结构.

我打了下面的测试代码:

#include <boost/multi_index_container.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/identity.hpp>

namespace mi = boost::multi_index;

struct t_hash{};

using elems = mi::multi_index_container<
    int,
    mi::indexed_by<
        mi::hashed_unique<
            mi::tag<t_hash>,
            mi::identity<int>
        >
    >
>;

int main() {
    elems es { 0x12, 0x34 };
    return 0; // set break point here and (gdb) p es
}
Run Code Online (Sandbox Code Playgroud)

https://wandbox.org/permlink/UtMfVRI4rT5AXUOZ

当我打印es时,(gdb) p es我得到以下输出:

$1 = {
  <boost::base_from_member<std::allocator<boost::multi_index::detail::hashed_index_node<boost::multi_index::detail::index_node_base<int, std::allocator<int> >, boost::multi_index::detail::hashed_unique_tag> >, 0>> = {
    member = {
      <__gnu_cxx::new_allocator<boost::multi_index::detail::hashed_index_node<boost::multi_index::detail::index_node_base<int, std::allocator<int> >, …
Run Code Online (Sandbox Code Playgroud)

c++ boost gdb multi-index

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

仅移动类型自适应std :: any和伪副本构造函数是安全的吗?

我想std::any使用仅移动类型变量进行初始化。我发现无法移动std :: any

编译错误情况

在通过链接的答案使用shared_ptr解决方法之前,我测试了以下代码:

#include <utility>
#include <iostream>
#include <any>

struct move_only {
    move_only() {
        std::cout << __PRETTY_FUNCTION__ << std::endl;
    }
    move_only(move_only const&) = delete;
    move_only(move_only &&) {
        std::cout << __PRETTY_FUNCTION__ << std::endl;
    }
};

int main() {
    move_only m;
    std::any a(std::move(m)); // error. copy constructor is required
}
Run Code Online (Sandbox Code Playgroud)

https://wandbox.org/permlink/h6HOSdgOnQYg4a6K

上面的代码由于move_only没有复制构造函数而输出编译错误。

添加副本构造函数进行测试

我添加了复制构造函数进行测试。

#include <utility>
#include <iostream>
#include <any>

struct move_only {
    move_only() {
        std::cout << __PRETTY_FUNCTION__ << std::endl;
    }
    move_only(move_only const&) { …
Run Code Online (Sandbox Code Playgroud)

c++ move c++17 stdany

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

可以在类范围中使用关键字"this"吗?

似乎我可以定义this在类范围内捕获的lambda表达式.至于我读N4640最新的工作草案,我找不到允许这种行为的句子.我想我错过了一些东西......

这是一个例子:

#include <iostream>
#include <functional>

struct foo {
    std::function<void()> f1 = [this]{ ++i; };
    int i = 0;
};

int main() {
    foo a;
    foo const& cref = a;
    cref.f1();
    std::cout << a.i << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

运行演示.(g ++ -std = c ++ 11 pedantic) https://wandbox.org/permlink/HPzaOxbBkOQOmuS6

更新

感谢@Brian和@cpplerner的评论,我理解我的基本问题是什么.那就是"关键字是否this允许在类范围中使用?不仅仅是非静态成员函数范围." 如果是,我可以this在类范围的lambda表达式捕获列表中使用.很清楚.

似乎this在类范围内视为非const指针.

为了解决我的基本问题,我读了N4640 9.2.2.1这个指针[class.this].似乎它在语法上是允许的,但是我找不到语义描述.对于非静态成员函数,我在9.2.2/3和9.2.2/4中找到了语义描述.

更新

我更新了问题的标题以代表我的基本问题.

原来的问题标题是Can lambda表达式在类范围内捕获了吗?

c++ lambda c++11

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

我什么时候应该使用 std::string / std::string_view 作为参数/返回类型

介绍

我正在编写一些通信应用程序。在 C++17(没有 Boost)之前,我使用std::string它的 const 引用作为cls1.

从 C++17 开始,我将std::string_view代码引入为cls2. 但是,我没有明确的政策何时应该使用std::string_view. 我的通信应用程序从网络接收数据并将其存储到recv_buffer. 并从recv_buffer.

建造

如果我只关注cls1的构造函数,移动构造是有效的。但我认为参数s来自哪里。如果它最初来自recv_buffer,我可以std::string_view在接收(很早)点创建。并且在recv_buffer的生命周期内启用,std::string_view随处使用。如果我需要存储部分recv_buffer然后创建std::string.

我注意到的唯一例外recv_buffer是始终包含我的应用程序类的完整数据。在这种情况下,移动构造是有效的。

吸气剂

我认为使用返回类型std::string_view具有优势。一些成员函数如是substr()高效的。但到目前为止,我没有看到任何缺点。

我怀疑我可能只看到std::string_view. 在重新编写许多代码之前,我想知道您的想法。

验证码

#include <string>

struct cls1 {
    explicit cls1(std::string s):s_(std::move(s)) {}
    std::string const& get() const { return s_; }
private:
    std::string s_;
}; …
Run Code Online (Sandbox Code Playgroud)

c++ string string-view c++17

5
推荐指数
2
解决办法
2424
查看次数

如何检查每个头文件是否包含所需的包含文件?

我正在使用 C++ 和 cmake 开发我的应用程序。

我想检查每个 C++ 头文件是否正确包含所需的包含文件。

下面是一个例子:

一个.hpp

inline void func_a() {
}
Run Code Online (Sandbox Code Playgroud)

b.hpp

// #include "a.hpp" is missing
inline void func_b() {
    func_a();
}
Run Code Online (Sandbox Code Playgroud)

主程序

#include "a.hpp"
#include "b.hpp"

int main() {}
Run Code Online (Sandbox Code Playgroud)

演示:https : //wandbox.org/permlink/kZqoNHMYARIB3bc1

b.hpp 应该包括 a.hpp。假设 b.hpp 缺少包括 a.hpp。如果 main.cpp 在 b.hpp 之前包含 a.hpp,则不会发生编译错误。如果包含顺序相反,则会发生编译错误。

我想检查一下这种问题。

我在 emacs 上使用 fly-check。它很好地检查了这个问题。我想在我的 cmake 构建系统中加入一些检查机制。

例如,如果我执行make depcheck,则检测到编译错误。

我认为如果我设置一个单独编译所有头文件但不链接的 cmake 目标,则会报告预期的编译错误。

到目前为止,我找不到如何设置它。

有没有办法做到这一点?或者其他方式来实现目标?

我的头文件包含策略

每个头文件都应该包含包含所需元素的头文件。换句话说,每个头文件都应该单独编译。

我想要达到的目标

我想知道通过工具辅助自动检测 b.hpp 缺少`#include "a.hpp" 的方法。该工具意味着不是编辑器。我猜 cmake 可以做到这一点。我正在努力寻找方法。

c++ dependencies cmake include

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

boost::multi_index 像 Rust 中的容器

我是 C++ 程序员。我尝试将一些使用 boost::multi_index 容器的 C++ 程序移植到 Rust。

https://www.boost.org/libs/multi_index/doc/index.html

C++代码使用multi_index通过一个索引得到的迭代器从所有索引中删除复杂度为O(1)的元素。这是重要的一点。

有什么好的方法可以使用 Rust 做到这一点吗?

我在标准库中找不到支持多个索引的容器。

c++ multi-index rust

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

类型检查 constexpr 函数用于检查 C++17 上的非模板类型或模板类型

我编写了类型检查 constexpr 函数。如果类型为type1ortype2则返回 true,否则返回 false。

这是代码。它按我的预期工作。

#include <type_traits>

struct type1{};
struct type2{};
struct type3{};

template <typename T>
constexpr bool is_type1or2() {
    return std::is_same_v<T, type1> || std::is_same_v<T, type2>;
}

static_assert(is_type1or2<type1>());
static_assert(is_type1or2<type2>());
static_assert(!is_type1or2<type3>());

int main(){}
Run Code Online (Sandbox Code Playgroud)

https://godbolt.org/z/dncKo1Pbb

现在,type1更改为具有非类型参数的模板。如何进行同类型检查?

#include <type_traits>

template <std::size_t N>
struct type1{};
struct type2{};
struct type3{};

template <typename T>
constexpr bool is_type1or2() {
    return std::is_same_v<T, type2>;
}

template <typename T, std::size_t N>
constexpr bool is_type1or2() {
    return std::is_same_v<T, type1<N>>;
}

// I want to …
Run Code Online (Sandbox Code Playgroud)

c++ type-traits c++17

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

如何使用 boost iostreams 流式解压

我正在使用 boost iostreams (1.64.0) 来解压缩 zlib 数据。我想做流式解压。这意味着压缩数据的大小不可预测。我编写了以下代码示例。

#include <sstream>
#include <string>
#include <iostream>

#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/zlib.hpp>

int main() {
    // Compress
    std::stringstream sender;
    boost::iostreams::filtering_streambuf<boost::iostreams::input> out;
    out.push(boost::iostreams::zlib_compressor());
    out.push(sender);
    sender << "Hello World";
    std::stringstream compressed;
    boost::iostreams::copy(out, compressed);

    // Decompress
    boost::iostreams::filtering_streambuf<boost::iostreams::input> in;
    in.push(boost::iostreams::zlib_decompressor());
    in.push(compressed);
    std::istream is(&in);
    std::size_t const buf_size = 256;
    char buf[buf_size] = { '\0' };
#if 0
    is.getline(buf, buf_size); // works fine
#else
    std::size_t read_size = is.readsome(buf, buf_size);
    std::cout << "read_size:" << read_size << std::endl;
#endif
    // http://www.cplusplus.com/reference/ios/ios/rdstate/ …
Run Code Online (Sandbox Code Playgroud)

c++ boost zlib boost-iostreams

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

如果返回值不是由左值保存,为什么主线程会等待 std::async() 创建的线程?

当我运行以下代码时,

#include <thread>
#include <iostream>
#include <future>

int main() {
    auto fut = std::async(
        std::launch::async, 
        []{
            std::this_thread::sleep_for(std::chrono::seconds(1));
            std::cout << "sub : " << std::this_thread::get_id() << std::endl;
        }
    ); 

    std::cout << "do some on main thread" << std::endl;
    
    fut.get();

    std::cout << "main: " << std::this_thread::get_id() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

我得到以下输出。

do some on main thread
sub : 139899103246080
main: 139899103250240
Run Code Online (Sandbox Code Playgroud)

运行演示:https://godbolt.org/z/c9WedY4oq

这与我预期的行为相同。“在主线程上执行一些操作”首先输出,因为创建的子线程在std::async()线程开始时等待 1 秒。

到目前为止,一切都很好。


然而,当我删除变量时fut,我得到了奇怪的行为。注意:此代码仅用于实验目的

do some on main thread
sub : 139899103246080
main: 139899103250240
Run Code Online (Sandbox Code Playgroud)

这是输出: …

c++ multithreading asynchronous future

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

仅当对象不存在于地图中时,如何使用 Rust 将新对象插入到地图中?

我正在将 C++ 代码传输到 Rust。这是原始的 C++ 代码。

#include <map>
#include <string>
#include <cassert>
#include <iostream>

int main() {
    std::map<std::string, int> m {
        { "A", 1 },
        { "B", 2 },
        { "D", 4 },
    };
    // *1
    auto r = m.equal_range("C"); // *2
    if (r.first == r.second) {
        auto const& it = r.first;
        assert(it->first == "D");
        assert(it->second == 4);
        // Let's say creating the object to insert is high cost
        // so it should be created only if the element doesn't …
Run Code Online (Sandbox Code Playgroud)

c++ dictionary insert rust

2
推荐指数
1
解决办法
2047
查看次数