标签: libstdc++

C++ STL:在创建std :: string的const副本时会改变c_str()吗?

这是关于STL实现处理const副本的区别的问题std::string.我有这么短的测试,它执行2个const副本并打印返回的地址c_str():

#include <stdio.h>
#include <string>
using namespace std;
int main()
{
          string a("Hello World!");
    const string b(a);
    const string c(b);

    printf("a: %p = %s\n", a.c_str(), a.c_str());
    printf("b: %p = %s\n", b.c_str(), b.c_str());
    printf("c: %p = %s\n", c.c_str(), c.c_str());
  return c.c_str() == b.c_str();
}
Run Code Online (Sandbox Code Playgroud)

在我的gcc 4.6.2上使用libstdc ++.so.6.0.16 STL所有指针都返回相等.

我可以依靠这种行为吗?

它是便携式还是最近标准中定义的?

这是否适用于当前或未来版本的libstdc ++,libc ++,Microsoft STL,stdcxx(apache.org)?

c++ stl libstdc++ c-str libc++

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

为什么 bernoulli_distribution::param_type 的构造函数是显式的?

在 §26.5.1.6/7 中它说:

未指定D::param_type是声明为(嵌套)类还是通过 typedef。在第 26.5 条中, 的声明 D::param_type采用 typedef 的形式,只是为了便于说明。

这似乎表明param_typeis 实现已定义。特别是对于 GCC 和 Clang,以下内容无法编译:

std::bernoulli_distribution d(0.50);
d.param(0.25);
Run Code Online (Sandbox Code Playgroud)

但是对于 MSVC 2013 来说确实如此。在 random.h 中查找 libstdc++,它显示param_type的构造函数被标记为显式:

struct param_type
{
  typedef bernoulli_distribution distribution_type;

  explicit
  param_type(double __p = 0.5)
  : _M_p(__p)
  {
_GLIBCXX_DEBUG_ASSERT((_M_p >= 0.0) && (_M_p <= 1.0));
  }

  double
  p() const
  { return _M_p; }

  friend bool
  operator==(const param_type& __p1, const param_type& __p2)
  { return __p1._M_p == __p2._M_p; }

private:
  double _M_p;
}; …
Run Code Online (Sandbox Code Playgroud)

c++ random libstdc++ c++11

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

`substr` 是否使用相同的底层内存?

我听说std::string使用底层引用计数器来避免复制std::string数据。

substr方法是使用它还是创建原始副本std::string

由于它是非常具体的实现,让我们首先关注 GNU 的实现。

c++ libstdc++

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

<unordered_set>中的错误?

我正在构建一个仅包含头的库(有充分理由;不要讨厌),它包含一个类和类成员函数的实现.这样做我遇到了一个非常奇怪的错误<unordered_set>.搜索GCC的Bugzilla似乎没有发现任何似乎解决这个问题.

我破坏(严重)的代码在我的命名空间中包含了包含.

namespace probability {

#include <string>
#include <unordered_set>  // only this include breaks
#include <unordered_map>  

class ProbabilityTools
{
...
Run Code Online (Sandbox Code Playgroud)

偶然的机会,我把#includes移到了类命名空间之外,并修复了问题<unordered_set>.当仅放置在命名空间内时,其他任何包都不会导致此问题<unordered_set>.

#include <string>
#include <unordered_set>   // works when outside the namespace
#include <unordered_map>

namespace probability {

class ProbabilityTools
{
...
Run Code Online (Sandbox Code Playgroud)

我正在使用GCC g ++ 4.8和-std = c ++ 11构建此代码,该代码在第二种配置中工作<unordered_map>,并且在两种配置中都可以使用.

这可能是一个libstdc ++ bug吗?GCC错误?

c++ libstdc++ c++-standard-library g++4.8

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

如何正确捆绑 libstdc++.so.*?

如何正确定位libstdc++.so.*用于编译 autotools 项目,以便将其与发行版捆绑在一起?

在我们团队切换到 C++11 后,我们需要将新的 libstdc++.so.6 与我们软件的每个发行版捆绑在一起,但是如何正确找到该库?

  • 赛格温: /lib/gcc/x86_64-pc-cygwin/5.2.0
  • Linux: /usr/lib64
  • 自定义安装: /usr/local/gcc-5.2.0/lib64

我已经尝试过:

install-exec-local:
    cp $(shell dirname $(shell which ${CXX}))/../lib64/libstdc++.so.6 ${prefix}/lib
Run Code Online (Sandbox Code Playgroud)

(而不是make dist,我们make install进入一个配置的前缀然后捆绑前缀)

这有效,但不适用于 Cygwin,我不确定它是否适用于其他平台,因此我的问题。

编辑

有关与软件捆绑的基本原理,请参阅此问题libstdc++.so它运作良好。我们还使用dlopen加载.so依赖的 s,libstdc++.so因此静态链接比听起来更难。

唯一的问题是定位libstdc++.so.6make dist时间(或我们的等价物),让我可以cp它为我们分发的${prefix}/lib目录,前焦油gzip压缩,并将其提供给客户。理想情况下,我正在寻找类似g++ --print-path-to-libstdc++.so.

运行软件的目标系统有一个较旧 libstdc++.so的 .

c++ autotools gnu-make libstdc++ c++11

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

gcc shared_ptr复制赋值实现

我正在shared_ptrGCC 5 中扫描实现,我看到以下内容:

  __shared_ptr&
  operator=(__shared_ptr&& __r) noexcept
  {
    __shared_ptr(std::move(__r)).swap(*this);
    return *this;
  }
Run Code Online (Sandbox Code Playgroud)

我的问题是为什么在交换之前临时的额外移动构造?我假设编译将删除任何额外的开销 - 但为什么不只是调用__r.swap(*this)?我缺少一些聪明的副作用吗?

我看到类中的其他函数也使用相同的模式实现,我可以理解接受const引用的情况,但是rvalue引用?

c++ gcc shared-ptr libstdc++ c++11

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

libstdc++ 中 std:shared_ptr 的原子交换如何正确

基于 libstdc++ 的原子交换实现(https://gcc.gnu.org/ml/libstdc++/2014-10/msg00154.html):

看起来静态分配了 16 个互斥锁。当需要发生原子交换时,交换代码会将要交换的 2 个指针散列到这些静态互斥锁、锁和交换中的一个(或两个)。但是,如果一个 shared_ptr 在线程之间共享并且正在被并发访问,那么互斥锁如何帮助保证并发访问和修改的同步?我猜代码假定内部原始指针正确对齐,但这是 x86 规则,而不是 C++ 保证。我缺少什么使交换原子性和正确性而无需为底层原始指针的每次读取额外锁定?

c++ gcc g++ libstdc++ c++11

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

当程序退出时,C ++库实现如何分配内存但不释放内存?

代码很简单:

#include <vector>
int main() {
    std::vector<int> v;
}
Run Code Online (Sandbox Code Playgroud)

然后,我在Linux上使用Valgrind构建并运行它:

g++ test.cc && valgrind ./a.out
==8511== Memcheck, a memory error detector
...
==8511== HEAP SUMMARY:
==8511==     in use at exit: 72,704 bytes in 1 blocks
==8511==   total heap usage: 1 allocs, 0 frees, 72,704 bytes allocated
==8511==
==8511== LEAK SUMMARY:
==8511==    definitely lost: 0 bytes in 0 blocks
==8511==    indirectly lost: 0 bytes in 0 blocks
==8511==      possibly lost: 0 bytes in 0 blocks
==8511==    still reachable: 72,704 bytes in …
Run Code Online (Sandbox Code Playgroud)

c++ memory-management libstdc++

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

无法从基于 libstdc++ 策略的数据结构中擦除

在 C++ 中有一个关联的容器,它实际上是一个集合(multiset),它可以给出其中元素的顺序。

这是我如何使用容器:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

template <typename T>
using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag,
                              tree_order_statistics_node_update>;
Run Code Online (Sandbox Code Playgroud)

问题是,我无法从中删除元素:

ordered_multiset<int> s;
s.insert(0);
s.erase(0);

cout << s.order_of_key(1); // returns number of elements less than x

// Outputs: 1
Run Code Online (Sandbox Code Playgroud)

奇怪的是,如果您替换less_equalless,那么您将能够毫无问题地完成这项工作,实际上如果您将容器用作多重集,那么您将无法从中删除元素,但是没有成套使用时的问题。

  • 导致问题的原因是什么?
  • 我该如何解决问题?

注意:请不要建议使用其他容器来解决问题。那不是解决办法。

c++ containers stl libstdc++

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

为什么 std::istream_iterator&lt; std::string_view &gt; 无法编译?

为什么 GCC 和 Clang 无法编译下面的代码片段(链接)?我想返回 s 的向量std::string_view,但显然无法string_view从 中提取 s stringstream

#include <iostream>
#include <sstream>
#include <string>
#include <string_view>
#include <vector>
#include <iterator>
#include <algorithm>
#include <ranges>


[[ nodiscard ]] std::vector< std::string_view >
tokenize( const std::string_view inputStr, const size_t expectedTokenCount )
{
    std::vector< std::string_view > foundTokens { };

    if ( inputStr.empty( ) ) [[ unlikely ]]
    {
        return foundTokens;
    }

    std::stringstream ss;
    ss << inputStr;

    foundTokens.reserve( expectedTokenCount );

    std::copy( std::istream_iterator< std::string_view >{ ss }, …
Run Code Online (Sandbox Code Playgroud)

c++ istream-iterator libstdc++ string-view

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