小编T.C*_*.C.的帖子

在C++中获取可变参数模板中的可变参数size_t ...参数的总和

我试图std::array在c ++中创建一个n维数组模板类(作为c ++数组的包装器),为整个n维数组分配一个数组块(避免使用n个数组和n个索引的开销).

在这样做时,我希望我的模板采用以下格式,sizes表示每个维度的大小.

template <class Type, size_t...  sizes>
class array_dimensional{
private:
    std::array<Type, /*here is the problem, how do 
       I get the product of all the sizes?*/> allocated_array;
...
Run Code Online (Sandbox Code Playgroud)

我的问题是我不知道如何获得所有尺寸的产品.

有可能这样做,如果是这样的话怎么样?

c++ arrays templates variadic-templates

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

Variadic模板展开到std :: tuple

我有一个过滤器类,它有两个模板参数,输入数和输出数.

template<int Ins, int Outs>
class Filter
{
    // implementation
};
Run Code Online (Sandbox Code Playgroud)

有时我需要串联多个过滤器,所以我想把它们包装在一个类中

template<int... args>
class Chain
{
};
Run Code Online (Sandbox Code Playgroud)

这样当我使用链条时

Chain<5, 10, 25, 15> chain;
Run Code Online (Sandbox Code Playgroud)

它将args展开成一个元组,最终在Chain类中得到类似的结果

std::tuple<Filter<5, 10>, Fiter<10, 25>, Filter<25, 15>> filters;
Run Code Online (Sandbox Code Playgroud)

这样的事情可能吗?我对这些概念很陌生,无法绕过它.

c++ templates variadic-templates c++11

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

为什么"auto"不能作为lambda参数接受

为什么这段代码会产生编译错误?

std::find_if(std::begin(some_list), std::end(some_list), [](const auto& item){
//some code
});
Run Code Online (Sandbox Code Playgroud)

当然在"自动"的错误?为什么不能自动知道类型?谢谢

c++ lambda auto c++11

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

简单的继承 - 奇怪的编译器错误

我有这个简单的代码片段,它包含struct timespec并添加静态成员的最小值和最大值.

#include <sys/stat.h>
#include <limits>
struct Timespec : public timespec {
    Timespec() :timespec() {};
    Timespec(decltype(tv_sec) s, 
                     decltype(tv_nsec) ns
            ) {
        tv_sec = s;
        tv_nsec = ns;
    }
    static const Timespec max;
    static const Timespec min;
};

const Timespec Timespec::min  = Timespec(0,0);
const Timespec Timespec::max  = Timespec(
        std::numeric_limits<decltype((timespec().tv_sec))>::max(), 
        std::numeric_limits<decltype((timespec().tv_nsec))>::max()
    );
Run Code Online (Sandbox Code Playgroud)

它编译OK,但如果我更换decltype((timespec()/*...*/decltype((Timespec()/*...*/在年底的两行,我得到:

$ make timespec.o
g++ -std=c++0x   -c -o timespec.o timespec.cc
In file included from timespec.cc:2:0:
/usr/include/c++/4.8/limits: In instantiation of ‘static constexpr _Tp std::numeric_limits<_Tp>::max() [with …
Run Code Online (Sandbox Code Playgroud)

c++ gcc c++11

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

C++ 14 shared_timed_mutex VS C++ 11互斥锁

我有一个8个线程之间的共享哈希表(我是一个8核PC),每个线程在哈希表中读写.

在示例1中,我使用了经典的互斥锁,并且在示例2中所有8个核心都是100%我使用了shared_timed_mutex,因为读取访问可以在竞争中但是所有8个核心都在40%

问题出在哪儿?

example 1:
mutex mutex_hash;

-- thread --
mutex_hash.lock();
//read
mutex_hash.unlock();
..
mutex_hash.lock();
//write
mutex_hash.unlock();
Run Code Online (Sandbox Code Playgroud)

============================

example 2:
shared_timed_mutex mutex_hash;

-- thread --
mutex_hash.lock_shared();
//read
mutex_hash.unlock_shared();
..
mutex_hash.lock();
//write
mutex_hash.unlock();
Run Code Online (Sandbox Code Playgroud)

c++ multithreading mutex c++14

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

将std :: tr1 :: shared_ptr与std :: function/std :: bind混合会导致编译器错误与更新的gcc

我需要使用一些旧的遗留代码std::tr1::shared_ptr.我需要包含的标题包含#include <tr1/memory>#include <tr1/functional>.在我的代码我想用std::functionstd::bind,因为我们的编译器支持这些.

这适用于gcc 4.6.3和4.7.3.在4.9.2,5.1.x和5.2.0中,这会导致编译器错误.它似乎是由于<tr1/functional>-include 而发生的.

我写了一个重现这个问题的小例子:

#include <tr1/memory>
#include <functional>
#include <tr1/functional>

struct Foo {
    void test(std::tr1::shared_ptr<int> i) {}
};

int main() {
    Foo f;
    std::function<void(std::tr1::shared_ptr<int>)> func = std::bind(&Foo::test, f, std::placeholders::_1);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

gcc-4.6.3,没有#include <tr1/functional>,编译好.在我的本地机器上测试过.

gcc-4.6.3,用#include <tr1/functional>,编译好.在我的本地机器上测试过.

gcc-5.1,没有#include <tr1/functional>,编译OK:http://ideone.com/XrkAXT

gcc-5.1,带#include <tr1/functional>编译器错误:http://ideone.com/sRWQLn

我没有使用任何std::tr1::function或根本没有std::tr1::bind,我从遗留代码中使用的唯一东西是一个std::tr1::shared_ptr对象.

为什么#include <tr1/functional>影响非tr1 …

c++ gcc tr1 c++11

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

std :: transform和std :: plus如何一起工作?

我正在阅读C++参考资料,并通过示例遇到了std :: plus函数.这很简单,只需添加lhs和rhs.代码是:

#include <functional>
#include <iostream>

int main()
{
   std::string a = "Hello ";
   const char* b = "world";
   std::cout << std::plus<>{}(a, b) << '\n';
}
Run Code Online (Sandbox Code Playgroud)

输出:Hello world

我改成了

#include <functional>
#include <iostream>

int main()
{
   int a = 5;
   int b = 1;
   std::cout << std::plus<int>{}(a, b) << '\n';
}
Run Code Online (Sandbox Code Playgroud)

输出:6

现在我做了

foo vector = 10 20 30 40 50
bar vector = 11 21 31 41 51
Run Code Online (Sandbox Code Playgroud)

我打了电话:

std::transform (foo.begin(), foo.end(), bar.begin(), foo.begin(), std::plus<int>());
Run Code Online (Sandbox Code Playgroud)

并且它给出了21 41 61 …

c++ c++11 c++14

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

c ++存储指针元组时遇到问题

我有一个对象'S'存储一个简单的指针元组,通过使用可变参数模板使其变得灵活.有两种方法,store()和store2().第一个(商店)工作正常.第二个将无法编译,因为std :: make_tuple失败并显示错误:

'调用'make_tuple'没有匹配函数

它进一步补充说,第一个参数没有从'B*'到'B*&&'的已知对话(这个错误在元组库头中很深).

代码在这里:

#include <tuple>
#include <utility>

template<typename...Rs>
struct S
{
    void store(std::tuple<Rs*...> rs)
    {
        rs_ = rs;
    }

    void store2(Rs*...rs)
    {
        rs_ = std::make_tuple<Rs*...>(rs...); // This is the guy that breaks
    }

private:
    std::tuple<Rs*...> rs_;
};

struct B
{};

struct A : S<B, B>
{};

int main()
{
    auto *b1 = new B;
    auto *b2 = new B;
    auto *a1 = new A;

    a1->store(std::make_tuple(b1, b2));   // This works
    a1->store2(b1, b2);    // How can I get this …
Run Code Online (Sandbox Code Playgroud)

c++ variadic-templates c++11

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

为什么rvalue不能分配给constexpr引用变量

我有以下代码

constexpr int into(int a,int b)
{
  int c=a*b;
  return c;
}

int main()
{
 constexpr int &n=into(5,5);

}
Run Code Online (Sandbox Code Playgroud)

我已阅读(在MSDN中)

该关键字constexpr是在C++ 11中引入的,并在C++ 14中进行了改进.这意味着不断表达.例如const,它可以应用于变量,以便在任何代码尝试修改该值时引发编译器错误.

在我阅读之后,我认为constexpr可以代替使用const,但对于上面的代码我得到一个编译器错误说明

`int main()':
invalid initialization of non-const reference of type 'int&' from an rvalue of type 'int'`
Run Code Online (Sandbox Code Playgroud)

什么时候constexpr更换const,它工作正常.我不明白这种行为; 有人可以解释一下吗?

c++ constexpr c++11 c++14

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

使用 enable_if 单独定义和声明模板成员函数,其模板参数还包括一个 constexpr 成员函数

以下不能在 CentOS 7 上的 g++ 8.1.0 下编译:

嘿.h

#pragma once
#include <iostream>
#include <type_traits>

class Valid {};
class Invalid {};

struct Hey
{
  template<typename T>
  static constexpr bool is_valid() { return std::is_same_v<T, Valid>; }

  template<typename T, std::enable_if_t<is_valid<T>()>* = nullptr>
    void howdy() const;
};

template<typename T, std::enable_if_t<Hey::is_valid<T>()>*>
  void Hey::howdy() const
{
  std::cout << "Howdy" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

编译器输出:

In file included from hey.cpp:1:
hey.h:18:8: error: no declaration matches ‘void Hey::howdy() const’
   void Hey::howdy() const
        ^~~
hey.h:14:10: note: candidate is: ‘template<class T, …
Run Code Online (Sandbox Code Playgroud)

c++ sfinae c++11

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