小编Yak*_*ont的帖子

c ++ 0x编译,但即使使用-gnu ++ 0x发现也会出现eclipse编辑器错误

我使用一些代码来报告任务的持续时间使用std :: chrono :: high_resolution_clock ... c ++ 0x的一部分.

我可以使用-gnu ++ 0x标志在eclipse cdt中成功编译c ++ 0x特性.虽然成功编译,编辑器似乎没有意识到c ++ 0x,即它在我的代码中显示任何c ++ 0x特性的错误.我通过在项目发现选项中添加-gnu ++ 0x标志来解决这个问题.注意:在您执行另一次编译并重建索引之前,不显示已修复...

-E -P -v -dD"$ {plugin_state_location} /specs.cpp"-std = gnu ++ 0x

我仍然有一个最后一个编辑器错误,我无法摆脱"符号'duration_cast'无法解决"(我有一张照片,但新用户无法发布照片)

任何人都有任何想法如何解决这个问题?这是代码:

#ifndef _scoped_timer_h_
#define _scoped_timer_h_

#include <iostream>
#include <chrono>
#include "boost/noncopyable.hpp"
#include "boost/format.hpp"

using namespace std::chrono;

  // Utility class for timing and logging rates
// (ie "things-per-second").
// NB _any_ destructor invokation (including early return
// from a function or exception throw) will trigger an
// output which …
Run Code Online (Sandbox Code Playgroud)

c++ eclipse boost c++11

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

C++ 11'native_handle'不是'std :: this_thread'的成员

在以下代码段中,

void foo() {
  std::this_thread::native_handle().... //error here
}

int main() {
  std::thread t1(foo);

  t1.join();
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

你如何获得的native_handlestd::this_thread从函数内foo

c++ c++11 stdthread

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

Boost :: Bind和虚函数重载:为什么它们有效?

我写了一些代码并且害怕它不起作用 - 所以我写了一个原型:

#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <iostream>

class base {
private:
    boost::function<void (int)> action;
protected:
    virtual void onDataBaseReady(int i) { std::cout << i << std::endl; }
public:
    void call() {
        action(10);
    }

    base() {
        action = boost::bind(&base::onDataBaseReady, this, _1);
    }
};

class child : public base {
protected:
    virtual void onDataBaseReady(int i) { std::cout << i+10 << std::endl; }
};

int main()
{
    static child c;
    c.call();
    std::cin.get();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

编译和工作.(输出20).但为什么?我也在VS2010下进行了测试,并想知道它是否适用于跨平台(比如在GCC下编译)?

主要是action = boost::bind(&base::onDataBaseReady, …

c++ boost boost-bind boost-function

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

如何使用任意参数作为模板参数的指针?

这是我在过去几天一直在研究的语义优化问题,我被困住了.我的真实程序运行在RTOS(特别是FreeRTOS)上,我需要生成任务(这是简单的,非终止版本的线程).C API接受void (*)(void*)任务的入口点和void*参数.非常标准的票价.

我为一个任务编写了一个包装类,而不是做一个老式的实现,比如有一个必须被最终任务类覆盖的虚方法,我宁愿让C++生成必要的参数存储通过可变参数模板和函数实现对象和粘合功能.

我已经用lambdas做了这个,std::function并且std::bind已经,但它们似乎实现了一些膨胀,即通过不解析函数目标直到运行时.基本上与虚拟方法相同的机制将使用.如果可能的话,我正试图减少所有开销.与硬编码实现相比,每个实例的膨胀大约为200字节.(这是在ARM Cortex-M3上,总闪存为128K,我们只剩下大约500个字节.)我在该主题上发现的所有SO问题同样推迟了函数的解析直到运行时.

这个想法是为了代码:

  1. 将可变参数的衰减版本存储在堆上分配的对象中(这是一种简化;可以使用分配器),并将其作为void*参数传递,
  2. 将生成的调用岛函数作为入口点,使用签名void(void*),使用存储的参数调用目标函数,以及
  3. (这是我无法弄清楚的部分)让编译器从目标函数的签名中推导出参数列表的类型,遵循不要重复自己的原则.
  4. 请注意,函数指针及其参数类型在编译时是已知的并且已解析,并且传递给函数的实际参数值在运行时才会知道(因为它们包括对象指针和运行时配置选项等).

在下面的例子中,我必须实例化的任务,一个Task<void (*)(int), bar, int> task_bar(100);我宁愿写Task<bar> task_bar(100);Task task_bar<bar>(100);并有编译器弄清楚(或者以某种方式告诉它在库)的可变参数必须符合指定函数的参数列表.

"明显的"答案是某种模板签名,template<typename... Args, void (*Function)(Args...)>但不用说,这不会编译.这种情况也不Function是第一个论点.

我不确定这是否可能,所以我在这里要求看看你们想出的是什么.为了简化问题,我省略了以对象方法而不是静态函数为目标的变体代码.

以下是具有代表性的测试用例.我用gcc 4.7.3和-std=gnu++11旗帜构建它.

#include <utility>
#include <iostream>
using namespace std;

void foo() { cout << "foo()\n"; }
void bar(int val) { cout << "bar(" << …
Run Code Online (Sandbox Code Playgroud)

c++ callback variadic-functions variadic-templates c++11

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

为什么using指令在全局范围和本地范围内表现不同?

当我编写以下代码时,它会被编译并正确执行:

#include <iostream>
using namespace std;

namespace first
{
  int x = 5;
  int y = 10;
}

namespace second
{
  double x = 3.1416;
  double y = 2.7183;
}

int main () {
  using namespace first; //using derective
  using second::y;
  cout << x << endl;
  cout << y << endl;
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

但是如果我使用main函数之外的指令编写如下,

using namespace first; //using derective
using second::y;
int main () {
  cout << x << endl;
  cout << y << endl;
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

它给出了这个编译错误: …

c++ namespaces

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

variadic templates与类中相同数量的函数参数

如何定义方法签名,以便它接受与variadic模板类定义相同数量的参数?例如,如何定义Array类:

template<typename T, int... shape>
class Array
{
public:
    T& operator () (???);
};
Run Code Online (Sandbox Code Playgroud)

所以你可以像这样调用它:

Array<int, 3, 4, 5> a;
a(1, 2, 3) = 2;
Run Code Online (Sandbox Code Playgroud)

c++ templates variadic-templates c++11

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

如何比较模板模板和模板实例?

首先,让我向您介绍一个部分解决方案:

template <template <class...> class,
        typename ...>
struct is_tbase_of:
  std::false_type
{ };

template <template <class...> class Type,
          typename ...Args>
struct is_tbase_of<Type, Type<Args...>>:
  std::true_type
{ };
Run Code Online (Sandbox Code Playgroud)

在一般情况下,它的工作原理:

is_tbase_of<std::vector, std::is_integral<int>>::value; // false
is_tbase_of<std::vector, std::vector<int>>::value;      // true
Run Code Online (Sandbox Code Playgroud)

但是,它不适用于"元返回"模板模板,例如:

template <template <class ...> class T>
struct quote
{
  template <typename ...U>
  using type = T<U...>;
};

using QVec =  quote<std::vector>;
is_tbase_of<QVec::template type, std::vector<int>>::value; // false...
Run Code Online (Sandbox Code Playgroud)

我尝试了很多东西,尝试获取第二个类型的模板参数(比较引用的类型特化)但似乎我无法让它们工作.即使专注is_tbase_ofquote(这将是一个不太通用但足够的选项)似乎将我发送到模板模式匹配的黑角.

c++ templates metaprogramming c++11 c++14

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

如果类具有特定成员函数,则启用模板函数

我编写了以下模板函数,它检查仲裁容器是否包含特定元素:

template<template<class, class...> class container_t, class item_t, class... rest_t>
bool contains(const container_t<item_t, rest_t...> &_container, const item_t &_item) {
    for(const item_t &otherItem : _container) {
        if(otherItem == _item) { return true; }
    }
    return false;
}
Run Code Online (Sandbox Code Playgroud)

这适用于大多数容器.然而,对于所有类型的集合(和地图),它是次优的,因为我们可以使用:

template<template<class, class...> class set_t, class item_t, class... rest_t>
bool contains(const set_t<item_t, rest_t...> &_set, const item_t &_item) {
    return _set.count(_item) > 0;
}
Run Code Online (Sandbox Code Playgroud)

显然,由于含糊不清,我们不能同时使用这两个模板.现在我正在寻找一种方法,用于std::enable_if启用第一个模板,如果container_t不提供count成员函数,第二个模板,如果它没有.但是我无法弄清楚如何检查特定成员函数(使用C++ 11).

c++ templates sfinae c++11

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

如果执行受保护的基础构造函数是noexcept,我怎么能说"noexcept"?

我们有这种情况,并想知道解决它的最佳方法

template<typename T>
struct A : T {
  A(T &&t) noexcept(noexcept(T(std::move(t))))
     :T(std::move(t))
  { }
};
Run Code Online (Sandbox Code Playgroud)

遗憾的是,这无法编译,因为T的移动构造函数受到保护,我们只允许在构造函数初始化列表中调用它*this.有什么办法使这项工作或甚至有一个标准的方法呢?

c++ inheritance protected noexcept c++11

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

为什么这个钻石类继承输出不是我所期望的?

考虑:

#include <iostream>

using namespace std;

class A {// base class
private:
    int data;
public:
    A(int data = 0) 
    {
        this->data = data;
    }
    void show() 
    {
        cout << data << endl;
        return;
    }
};

class B : virtual public A {
public:
    B(int data = 0) :
        A(data) {
    }
};

class C : virtual public A {
public:
    C(int data = 0) :
        A(data) {
    }
};

class D : public B, public C {
public:
    D(int dataB …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance

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