小编Pra*_*ian的帖子

boost ::以下代码的任何替代品

我希望摆脱对我的代码的boost依赖.我有以下结构构造.在代码中的另一个地方调用和使用此结构时boost::any_cast.我知道模板类会这样做,但发现很难写这个模板. - C++新秀.

 struct Properties {
 public:
 Properties() {}
 Properties(const std::string &s, const boost::any & p) {
      name = s;
      value = p;
 }

 template <typename T>
 Properties(T n) {
      value = n;
 }
 boost::any value;

 std::string name;
};
Run Code Online (Sandbox Code Playgroud)

c++ templates boost casting boost-any

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

std :: vector vs std :: list表示插入频率和动态大小

考虑我要向容器添加随机数量的项目的情况,即无法预测容器的大小,插入的频率高,插入应该在容器的末尾,除此之外我想删除一个几乎恒定时间的元素.

注意:我还想在共享内存中使用列表或向量.

所以在这种情况下,最好使用std :: vector或std :: list?

c++ list vector shared-memory c++11

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

static_assert中的std :: pow会触发错误C2057吗?

Visual Studio 2013中的以下代码导致错误C2057:

#include <cmath>

int main() {
    static_assert(std::pow(2, 2) < 5, "foobar");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

错误C2057:预期的常量表达式

如果我在GCC下编译-std=c++0x它可以正常工作.http://ideone.com/2c4dj5

如果我更换std::pow(2, 2)4,也编译的Visual Studio 2013下.

  • 这是VS2013的错误吗?
  • 我该如何解决这个问题?

c++ static-assert c++11 visual-studio-2013

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

C++ 11核心语言是否负责Singleton Dead Reference?

我最近读过Andrei Alexandrescu的Modern C++ Design.在阅读了6.章后,我开始担心我们公司的单身人士.由于我们经验丰富的团队负责人编写核心助手库,如单身人士等.我问他处理单身人员的方式是否能解决死亡参考问题?如果他使用了由C核心语言给出的at_exit函数调用?

他告诉我C++ 11具有单例支持,并将连续执行CTOR和DTOR,它们不会是任何死引用问题.用户不必处理同步.

即使它听起来很棒我找不到任何在互联网上证实他的信息.那么请告诉我C++ 11是否为单身人士处理死亡参考问题,如果是这样,请解释一下后面的黑暗魔法是什么?

c++ singleton c++11

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

重载<<运算符以插入向量

我找到了许多解决方案,它们使输出流操作符超载以打印出一个向量.我需要做相反的事情.就像是,

vector<string> v;
  v << "String1" << "String2" << "String3" << "String4" << "String5";
Run Code Online (Sandbox Code Playgroud)

我有这个代码,只添加第一个字符串.我明白为什么会这样,但我无法弄清楚如何添加其他字符串.

template<typename T, typename T2>
vector<T> operator<<(vector<T>& v1, T2 s) {
    v1.push_back(s);
    return v1;
}
Run Code Online (Sandbox Code Playgroud)

c++ vector operator-overloading

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

在C中传递具有MISRA合规性的缓冲区

MISRA令我们的开发人员感到沮丧.

我们得到MISRA错误"不要将指针算法应用于指针"和"指针不指向数组".

我们使用以下语法:

uint8_t const * p_buffer
Run Code Online (Sandbox Code Playgroud)

将缓冲区传递给将缓冲区写入SPI总线的函数.

给出一个示例代码片段:

static void Write_Byte_To_SPI_Bus(uint8_t byte);

void Write_Buffer_To_SPI_Bus(uint8_t const * p_buffer,
                             unsigned int quantity)
{
  for (unsigned int i = 0; i < quantity; ++i)
  {
    Write_Byte_To_SPI_Bus(*p_buffer++);
  }
}
Run Code Online (Sandbox Code Playgroud)

有没有办法让一个指向数组中单元格的指针并增加它以满足MISRA?

我的解释是MISRA想要将索引递增到数组而不是指针:

void Write_Array_To_SPI_Bus(uint8_t const p_array[],
                            unsigned int quantity)
    {
      for (unsigned int i = 0; i < quantity; ++i)
      {
        Write_Byte_To_SPI_Bus(p_array[i]);
      }
    }
Run Code Online (Sandbox Code Playgroud)

许多开发人员都是老派,他们更喜欢使用指针uint8_t而不是传递数组.

c arrays pointers misra

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

C++:std :: move with rvalue reference不是移动内容

示例程序:

#include <iostream>
#include <string>
#include <vector>

template <typename T>
void print(const T& _vec)
    {        
        for( auto c: _vec )
            std::cout << c << ",";
    }

typedef std::vector<std::string> vecstr_t;

struct Trade
{
    explicit Trade(vecstr_t&& vec) : _vec(vec )
    {       
    }  

     vecstr_t _vec;
};


int main()
{   
    vecstr_t tmpV = {"ONE", "TWO", "THREE", "FOUR"};    
    std::cout << "size 1:" << tmpV.size() << "\t"; print(tmpV); std::cout <<  "\n" ;    
    Trade t(std::move(tmpV));    
    std::cout << "size 2:" << tmpV.size() << "\t";  print(tmpV); std::cout <<  "\n" …
Run Code Online (Sandbox Code Playgroud)

c++ move move-semantics c++11

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

自动绑定lamba函数的寿命是多少?

如果自动绑定到lambda函数,自动生命的生命是否延伸到lambda函数的生命周期?

最简单的情况:

auto genfunc (int start)
{
     int count=start;
     return [&count] {
         return count++;
     };
}
Run Code Online (Sandbox Code Playgroud)

这是好的还是未定义的行为?

c++ lambda c++11 c++14

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

如何计算C++中向量的重复条目

我正在使用Armadillo在C++中进行线性代数计算.

例如,有一个

vector a = (1,1,2,2,0,2,1,0)
Run Code Online (Sandbox Code Playgroud)

我希望返回一个矩阵

(0, 2) //means 0 shows 2 times in the vector
(1, 3) //1 shows 3 times
(2, 3) //2 shows 3 times
Run Code Online (Sandbox Code Playgroud)

有什么功能可以完成这样的工作吗?

c++ armadillo

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

对于不应该是私有的功能,抛出了“在这种情况下是私有的”(GCC 5.3.0,C ++ 11)

我正在尝试创建一个队列,这需要创建存储在队列中的另一个对象。错误是

binary.cpp: In function ‘int main()’:
binary.cpp:183:1: error: ‘Queue<T>::Queue(T) [with T = binary<std::basic_string<char> >*]’ is private
 Queue<T>::Queue(T item){
 ^
binary.cpp:286:65: error: within this context
  Queue<binary<string>*>* queue = new Queue<binary<string>*>(tree);
                                                                 ^
Run Code Online (Sandbox Code Playgroud)

binary.cpp: In instantiation of ‘Queue<T>::Queue(T) [with T = binary<std::basic_string<char> >*]’:
binary.cpp:286:65:   required from here
binary.cpp:132:1: error: ‘Link<T>::Link(T) [with T = binary<std::basic_string<char> >*]’ is private
 Link<T>::Link(T item){
 ^
binary.cpp:184:7: error: within this context
  head = new Link<T>(item);
Run Code Online (Sandbox Code Playgroud)

第一个是队列的实例化,第二个来自队列的构造函数,在第一个错误的实例化行中调用该构造函数。重要的声明和定义是:

template<class T>
class Link{
    Link(T item);

    private:
    T content;
    Link<T>* next;
}; …
Run Code Online (Sandbox Code Playgroud)

c++ gcc c++11

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