小编Rom*_*man的帖子

如何在C++ 11中访问可能不存在的类型别名?

我想写这样的东西:

template <class T>
using MyTypeOrTupple = typename std::conditional<has_member_type_MyType<T>::value,
                                                 typename T::MyType,
                                                 std::tuple<> >::type;
Run Code Online (Sandbox Code Playgroud)

has_member_type_MyType使用https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Member_Detector实现

然而,海湾合作委员会(4.8.4)仍然抱怨使用T::MyType何时MyType未定义T.有办法解决这个问题吗?

c++ templates c++11

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

禁止将rvalue引用传递给函数

我们有以下便利功能,可以从地图中获取值,或者如果找不到键,则返回后备默认值.

template <class Collection> const typename Collection::value_type::second_type&
    FindWithDefault(const Collection& collection,
                    const typename Collection::value_type::first_type& key,
                    const typename Collection::value_type::second_type& value) {
      typename Collection::const_iterator it = collection.find(key);
      if (it == collection.end()) {
        return value;
      }
      return it->second;
    }
Run Code Online (Sandbox Code Playgroud)

这个函数的问题是它允许传递一个临时对象作为第三个参数,这将是一个bug.例如:

const string& foo = FindWithDefault(my_map, "");
Run Code Online (Sandbox Code Playgroud)

是否可以通过使用std :: is_rvalue_reference和static assert以某种方式禁止将rvalue引用传递给第三个参数?

c++ stl c++11

10
推荐指数
1
解决办法
1073
查看次数

haproxy中每个后端的maxconn限制

我们的haproxy负载均衡器为其后端打开了数千个连接,即使它的设置说每个服务器实例打开不超过10个连接(见下文).当我取消注释"选项http-server-close"后端连接的数量下降但是我希望保持活动后端连接.

为什么maxconn不受尊重http-keep-alive?我验证ss了打开的后端连接处于ESTABLISHED状态.

defaults
     log global
     mode    http
     option http-keep-alive
     timeout http-keep-alive 60000
     timeout connect 6000
     timeout client  60000
     timeout server  20000


 frontend http_proxy
     bind    *:80
     default_backend backends

 backend backends
     option prefer-last-server

     # option http-server-close
     timeout http-keep-alive 1000
     server s1 10.0.0.21:8080 maxconn 10
     server s2 10.0.0.7:8080  maxconn 10
     server s3 10.0.0.22:8080 maxconn 10
     server s4 10.0.0.16:8080 maxconn 10
Run Code Online (Sandbox Code Playgroud)

connection networking load-balancing haproxy

10
推荐指数
1
解决办法
2193
查看次数

atomic fetch_add vs添加性能

下面的代码演示了多线程编程的好奇心.特别std::memory_order_relaxed是单个线程中增量与常规增量的性能.我不明白为什么fetch_add(宽松)单线程比常规增量慢两倍.

static void BM_IncrementCounterLocal(benchmark::State& state) {
  volatile std::atomic_int val2;

  while (state.KeepRunning()) {
    for (int i = 0; i < 10; ++i) {
      DoNotOptimize(val2.fetch_add(1, std::memory_order_relaxed));
    }
  }
}
BENCHMARK(BM_IncrementCounterLocal)->ThreadRange(1, 8);

static void BM_IncrementCounterLocalInt(benchmark::State& state) {
  volatile int val3 = 0;

  while (state.KeepRunning()) {
    for (int i = 0; i < 10; ++i) {
      DoNotOptimize(++val3);
    }
  }
}
BENCHMARK(BM_IncrementCounterLocalInt)->ThreadRange(1, 8);
Run Code Online (Sandbox Code Playgroud)

输出:

      Benchmark                               Time(ns)    CPU(ns) Iterations
      ----------------------------------------------------------------------
      BM_IncrementCounterLocal/threads:1            59         60   11402509                                 
      BM_IncrementCounterLocal/threads:2            30         61   11284498                                 
      BM_IncrementCounterLocal/threads:4            19         62   11373100                                 
      BM_IncrementCounterLocal/threads:8            17 …

c++ multithreading c++11

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

使用 SIMD 将 ascii 字符串位打包为 7 位二进制 blob

相关:bitpack ascii string into 7-bit binary blob using ARM-v8 Neon SIMD - 同样的问题专门针对 AArch64 内在函数。这个问题涵盖了可移植的 C 和 x86-64 内在函数。


我想将 char 字符串编码为 7 位 blob,以减少 12.5% 的内存。我想尽可能快地完成它,即在编码大字符串时以最小的延迟。

这是该算法的简单实现:

void ascii_pack(const char* ascii, size_t len, uint8_t* bin) {
  uint64_t val;
  const char* end = ascii + len;

  while (ascii + 8 <= end) {
    memcpy(&val, ascii, 8);
    uint64_t dest = (val & 0xFF);

    // Compiler will perform loop unrolling
    for (unsigned i = 1; i <= 7; ++i) {
      val …
Run Code Online (Sandbox Code Playgroud)

c ascii sse simd intrinsics

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

无法在python中使用gcs进行身份验证

我按照https://developers.google.com/storage/docs/gspythonlibrary#credentials中的示例进行操作

我通过在开发中选择来创建客户端/秘密对.控制台"创建新的客户端ID","已安装的应用程序","其他".

我的python脚本中有以下代码:

import boto
from gcs_oauth2_boto_plugin.oauth2_helper import SetFallbackClientIdAndSecret
CLIENT_ID = 'my_client_id'
CLIENT_SECRET = 'xxxfoo'
SetFallbackClientIdAndSecret(CLIENT_ID, CLIENT_SECRET)

uri = boto.storage_uri('foobartest2014', 'gs')
header_values = {"x-goog-project-id": proj_id}
uri.create_bucket(headers=header_values)
Run Code Online (Sandbox Code Playgroud)

它失败并出现以下错误:

   File "/usr/local/lib/python2.7/dist-packages/boto/storage_uri.py", line 555, in create_bucket
      conn = self.connect()
    File "/usr/local/lib/python2.7/dist-packages/boto/storage_uri.py", line 140, in connect
      **connection_args)
    File "/usr/local/lib/python2.7/dist-packages/boto/gs/connection.py", line 47, in __init__
      suppress_consec_slashes=suppress_consec_slashes)
    File "/usr/local/lib/python2.7/dist-packages/boto/s3/connection.py", line 190, in __init__
      validate_certs=validate_certs, profile_name=profile_name)
    File "/usr/local/lib/python2.7/dist-packages/boto/connection.py", line 572, in __init__
      host, config, self.provider, self._required_auth_capability())
    File "/usr/local/lib/python2.7/dist-packages/boto/auth.py", line 883, in get_auth_handler
      'Check your credentials' % …
Run Code Online (Sandbox Code Playgroud)

python boto google-cloud-storage gsutil

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

多次加入同一个线程

我的程序中的流程从同一个调用线程调用了pthread_join(thread_id, nullptr); 2 次thread_id

第一次调用成功返回,但第二次它只是无限期挂起。文档没有明确说明禁止对同一个thread_id多次调用pthread_join。真的是这样吗?

我知道我可以使用 pthread_cond_t 实现线程同步,但我想先了解 pthread_join 的限制。

c linux posix pthreads

4
推荐指数
1
解决办法
3335
查看次数

在C++中将char-array转换为POD数组

编写下面代码的正确方法是什么?

我有一个内存管理器,它提供了我char *的,但我需要使用数组uint32_t.我如何解决严格的别名规则?我理解,对于单个对象,建议只复制内容,memcpy()但对于一组对象,该解决方案是不可接受的.

char* ptr = manager()->Allocate(1000 * sizeof(uint32_));
uint32_t* u32ptr = reinterpret_cast<uint32_t*>(ptr);
....
u32ptr[x] = y;
Run Code Online (Sandbox Code Playgroud)

c++ pointers dynamic-memory-allocation reinterpret-cast c++11

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

仅选择几行时bigquery收费过高

从logs.nobids_05限制1中选择DATE(request_time),使我“处理了3.48 GB”,考虑到request_time是出现在每行中的一个字段,这有点多了。

在许多其他情况下,仅触摸列会自动将其总大小添加到成本中。例如,

select * from logs.nobids_05 limit 1
Run Code Online (Sandbox Code Playgroud)

给我“此查询在运行时将处理274 GB”。我确定bigquery不需要读取274GB即可输出1行数据。

google-bigquery

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