小编Dra*_*rax的帖子

什么是string_view?

string_view是C++ Library Fundamentals TS(N3921)中添加到C++ 17中的一个提议特性

据我所知,它是一种代表某种字符串"概念"的类型,它是任何类型的容器的视图,可以存储可视为字符串的东西.

  • 这是正确的吗 ?
  • 规范const std::string&参数类型应该 变成string_view吗?
  • 还有另一个重要的问题string_view需要考虑吗?

c++ fundamentals-ts string-view c++17

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

为什么我们不能在类中声明命名空间?

在类中声明一个类是有效的.(嵌套类)

在类中声明命名空间无效.

问题是:是否有任何好的理由(除了c ++语法/语法问题)禁止在类中声明命名空间?


至于我为什么要这样做,这里有一个例子:

让我们对二叉树容器进行基本的转换

template<typename Data>
class binary_tree
{
 public:
  ... stuff ....     

 private:
  ... iterators class declaration ...

 public:
  typedef left_depth_iterator_impl     left_depth_iterator;
  typedef right_depth_iterator_impl    right_depth_iterator;
  typedef left_breadth_iterator_impl   left_breadth_iterator;
  typedef right_breadth_iterator_impl  right_breadth_iterator;

  ... stuff ....     

 private:
  Data         data;
  binary_tree* left;
  binary_tree* right;
};
Run Code Online (Sandbox Code Playgroud)

现在我注意到我的类中有很多迭代器,所以我想在同一个命名空间中重新组合它们,如下所示:

template<typename Data>
class binary_tree
{
 public:
  ... stuff ....     

 private:
  ... iterators class declaration ...

 public:
  namespace iterator
  {
    typedef left_depth_iterator_impl     left_depth;
    typedef right_depth_iterator_impl    right_depth;
    typedef left_breadth_iterator_impl   left_breadth;
    typedef right_breadth_iterator_impl  right_breadth;
  }

  ... stuff …
Run Code Online (Sandbox Code Playgroud)

c++ namespaces class

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

如何创建一个可变的通用lambda?

从C++ 14开始,我们可以使用泛型lambdas:

auto generic_lambda = [] (auto param) {};
Run Code Online (Sandbox Code Playgroud)

这基本上意味着它的调用操作符基于标记为auto的参数进行模板化.

问题是如何创建一个可以接受可变参数数量的lambda,类似于可变参数函数模板的工作方式?如果这不可能,最接近的东西可以用同样的方法吗?你会怎么存储它?有可能std::function吗?

c++ lambda variadic c++14

37
推荐指数
3
解决办法
2万
查看次数

工会可以模板化吗?

似乎联合可以在c ++ 11中进行模板化,例如在std :: optional的参考实现中使用它们.

那可能在c ++ 11之前吗?

c++ templates unions

26
推荐指数
2
解决办法
4639
查看次数

自动将推送的文件从一个GitHub存储库复制到另一个存储库

我有两个GitHub存储库.

我想自动(可能使用钩子和/或github API)提交并将文件推送到第二个存储库,当它们被推送到第一个存储库时.

第二个存储库不是第一个存储库的克隆,它们的文件夹布局不一定相同,只有一堆共同的文件.

这样做最简单的方法是什么?

如果我不需要安装http服务器或学习perl,奖励积分:)

git github githooks github-api

23
推荐指数
4
解决办法
6766
查看次数

Template specialization on template member of template class

This is probably only a syntax problem.

So i have this template class :

template <typename String, template<class> class Allocator>
class basic_data_object
{
  template<typename T>
  using array_container = std::vector<T, Allocator<T>>;
};
Run Code Online (Sandbox Code Playgroud)

And another one :

template <typename String, template<class> class Allocator, typename T>
struct get_data_object_value
{
};
Run Code Online (Sandbox Code Playgroud)

Now i want to specialize the second one's T parameter with the first one's inner typedef array_container for any given type.

template <typename String, template<class> class Allocator, typename T>
struct get_data_object_value
<String, Allocator, …
Run Code Online (Sandbox Code Playgroud)

c++ templates specialization c++11

19
推荐指数
3
解决办法
1530
查看次数

为什么没有任何std :: stoui?

从c ++ 11开始,<string>标头提供:

从转换为std::string有符号整数。

我们也有他们的未签名副本:

现在,即使是一个孩子,也会注意到某些东西丢失了吗?:)

因此问题是:是否有理由不提供std::stoui或仅仅是被遗忘的东西(基本上,这种“显而易见的”东西怎么会被遗忘)?

此外,是否意味着将一个正确的方式std::stringunsigned int为:

unsigned int ui = static_cast<unsigned int>(std::stoul(std::string{"42"}));
Run Code Online (Sandbox Code Playgroud)

c++ stl c++11

7
推荐指数
0
解决办法
1874
查看次数

Boost池分配器比新的慢

所以我memory_pools基于boost池创建了这个容器分配器类:

memory_pools.hpp

#ifndef MEMORY_POOL_HPP
# define MEMORY_POOLS_HPP

// boost
# include <boost/pool/pool.hpp>
# include <boost/unordered_map.hpp>

template<typename ElementType>
class   memory_pools
{
public:
  template <typename>
  friend class memory_pools;

private:
  using pool = boost::pool<>;

public:
  using value_type = ElementType;
  using pointer = value_type*;
  using const_pointer = const value_type*;
  using reference = value_type&;
  using const_reference = const value_type&;
  using size_type = pool::size_type;
  using difference_type = pool::difference_type;

public:

  template<typename OtherElementType>
  struct rebind
  {
    using other = memory_pools<OtherElementType>;
  };

public:
  memory_pools();

  template<typename SourceElement>
  memory_pools(const …
Run Code Online (Sandbox Code Playgroud)

c++ performance performance-testing boost-pool

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

c ++中的rvalues如何存储在内存中?

努力学习lvalues,rvalues并为他们分配内存.因此,有很多学习材料会有一些混乱.

An rvalue是一个值,只需要在创建它的表达式的边界中存在(至少在C++ 11之前).所以它有一个占据的地址和内存块.但根据定义,我们无法得到一个地址rvalue,因为它与一个临时对象形成鲜明对比lvalue.但即使在C++ 11之前,我们也可以rvalue通过从函数返回它并将其保存为const引用类型来获取地址(呃,我猜不是地址而是值).

那么,更确切地说,rvalue分配如何运作?程序或操作系统在多长时间内真正记住了rvalue创建并标记为已分配的内存位置,而另一个对象无法取代它?

我如何看待,现在rvalues存储就像lvalues我们只是有其他权限来访问它们.并且它们具有其他类型的释放 - 用于lvalues超出范围,rvalues可以通过存在于表达式边界中或者直到没有更多链接来优化.

c++ memory rvalue rvalue-reference c++11

7
推荐指数
2
解决办法
1405
查看次数

什么是节点句柄?

在随机文档检查期间,cppreference.com我注意到一些容器的新成员函数重载作为参数,恰好是一个新的标准类型,C++17称为a Node Handle.

现在,同一站点上的节点句柄的文档页面提供了有关如何工作的多个细节和技术行为,但它并没有真正正确地传达这种新类型的一般概念和目的.

因此,问题是什么是节点句柄?

c++ c++17

7
推荐指数
2
解决办法
1055
查看次数