小编Ind*_*ant的帖子

为什么boost regex会耗尽堆栈空间?

#include <boost/regex.hpp>
#include <string>
#include <iostream>

using namespace boost;
static const regex regexp(
        "std::vector<"
            "(std::map<"
                   "(std::pair<((\\w+)(::)?)+, (\\w+)>,?)+"
             ">,?)+"
        ">");

std::string errorMsg =
"std::vector<"
        "std::map<"
                "std::pair<Test::Test, int>,"
                "std::pair<Test::Test, int>,"
                "std::pair<Test::Test, int>"
        ">,"
        "std::map<"
                "std::pair<Test::Test, int>,"
                "std::pair<Test::Test, int>,"
                "std::pair<Test::Test, int>"
        ">"
">";
int main()
{
    smatch result;
    if(regex_match(errorMsg, result, regexp))
    {  
        for (unsigned i = 0; i < result.size(); ++i)
        {  
            std::cout << result[i] << std::endl;
        }
    }

//    std::cout << errorMsg << std::endl;

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

这会产生:

terminate called after throwing …
Run Code Online (Sandbox Code Playgroud)

c++ regex boost boost-regex

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

c ++中抛出的对象在哪里?

我想弄清楚我抛出的对象存储在内存中的位置.所以我为它写了一个小程序:

#include <iostream>

#define print_dist() int a;\
    do { std::cout << __FUNCTION__ << "() a[" << (long)&a - (long)ptrMainStackBase << "]" << std::endl; } while (0)

#define print_distx(x) \
    do { std::cout << __FUNCTION__ << "() " #x "[" << (long)&x - (long)ptrMainStackBase << "]" << std::endl; } while (0)

#define print_distxy(x, y) \
    do { std::cout << __FUNCTION__ << "() " #x "(ex)[" << (long)&x - (long)y << "]" << std::endl; } while (0)

class CTest
{
    public:
        CTest() …
Run Code Online (Sandbox Code Playgroud)

c++ exception

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

在C++中生成HTML输出的最佳方法是什么?

可能重复:
C++ HTML模板框架,模板化库,HTML生成器库

我有一个包含许多表的程序,所以我想在调试程序时在日志文件中跟踪它们.我想要彩色的格式化输出,所以我想我是用HTML格式编写的.那么用C++创建HTML文件的最佳方法是什么?

琐碎的方式是如此恶心和容易出错:

std::stringstream ret;
ret << "<TABLE BORDER=\"1\" CELLBORDER=\"0\">\n";
ret << "    <TR>\n";
...
Run Code Online (Sandbox Code Playgroud)

所以我认为我写了一个HTML包装器,但我认为有这么多原因.

我希望在C++中使用这种语法或类似的静态类型HTML包装器:

CHtmlHtml Html;
Html <<
    Body() % Id("1")
        << H1("My First Heading") <<
        << P("My first paragraph.");
Run Code Online (Sandbox Code Playgroud)

或者在这种情况下

CHtmlTable table;
table % Border(1) % CellBorder(0) <<
    Tr() <<
        Td("Text") % Colspan(4);
Run Code Online (Sandbox Code Playgroud)

有没有类似的项目?

html c++ html-helper wrapper

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

使用std :: list作为循环列表是否安全?

那么递增或递减end()迭代器是在标准中定义的吗?在linux上,begin()实现为end()++.

#include <list>
#include <iostream>

int main()
{
  std::list<int> numbers;
  for (int i = 0; i < 10; i++)
    numbers.push_back(i);

  auto it = numbers.begin();
  int count = 3;
  while (count)
  {
    std::cout << *it++;
    if (it == numbers.end())
    {
      ++it; // is this ok ???
      --count;
      std::cout << '\n';
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

那么每个平台的输出总是一样的吗?

输出:

0123456789
0123456789
0123456789
Run Code Online (Sandbox Code Playgroud)

c++ iterator list std

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

为什么我没有得到关于在ctor中访问未初始化成员变量的编译器警告?

这是一个简单的测试用例,无需任何警告即可编译.看起来像一个常见的错误,但clang,gcc和visual studio在这种情况下不会发出警告.为什么?

class Image {
  private:
    int width, height;
    int* array;
  public:
    Image(int _width, int _height);
    void crashTest();
};
Image::Image(int _width, int _height)
{
  array = new int[width * height];
               // ^^^^^   ^^^^^^ this is wrong
               // I expect a warning here e.g.: 'width is uninitialized here'
  width = _width;
  height = _height;
}
void Image::crashTest()
{
  for (int x = 0; x < width; ++x)
  {
    for (int y = 0; y < height; ++y)
      array[x + y * …
Run Code Online (Sandbox Code Playgroud)

c++ constructor initialization compiler-warnings

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

如何在外部服务器上构建我的本地git仓库?

在我们公司,我们拥有非常强大的基于Linux的构建服务器(具有40核的双Xeon)和不那么强大的win7笔记本电脑.我们用C/C++语言构建我们的产品,用于深奥的CPU.编译器仅存在于Linux中.我可以用Qt Creator编辑我的git repo.它工作得很快,一切都很快.但我无法在笔记本电脑上构建源代码.我们有一个主要的git repo,我可以将相同的repo克隆到我的笔记本电脑和构建服务器.当我按下构建按钮时,我希望实现这一点,我的代码在构建服务器上神奇地构建.我做了一个概念证明解决方案,其中我的构建脚本在我的repo上执行git diff并将其scp到构建服务器,而不是ssh构建服务器在服务器repo上应用diff而不是启动并等待编译.但是这个解决方案并不是那么简单.我认为存在更好的接近/方法.那么如何在外部服务器上构建我的git repo?

c++ git compilation

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

使用operator!= as作为运算符<在std :: set中是否安全?

我有一个带有一些成员的结构,我有一个实现的运算符==.在operator ==的帮助下实现运算符是否安全?我想在一个集合中使用这个结构,我想检查这个结构是否是唯一的.

struct Data
{
  std::string str1;
  std::string str2;
  std::string str3;
  std::string str4;

  bool operator==(const Data& rhs)
  {
    if (str1 == rhs.str1
     && str2 == rhs.str2
     && str3 == rhs.str3
     && str4 == rhs.str4
       )
      return true;
    else
      return false;
  }

  // Is this ok??
  bool operator<(const Data& rhs)
  {
    return !this->operator==(rhs);
  }
}
Run Code Online (Sandbox Code Playgroud)

所以当我将这个结构插入到std :: set时会发生什么?

c++ algorithm set

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

如何为 lambda 创建唯一类型?

我想为仅从第二次迭代运行的 for 循环创建一个辅助函数,因为我有很多具有以下模式的代码:

firstItem = true;
for (unsigned i = 0; i < 5; ++i)
{
  firstItem ? firstItem = false : std::cout << ",\n";
  std::cout << i;
}
Run Code Online (Sandbox Code Playgroud)

我开始考虑一个辅助函数,并提出了这个解决方案:

template <typename T>
void fromSecondIter(T fn)
{
  static bool firstItem = true; // because of this it works with unique types only
  if (firstItem)
    firstItem = false;
  else
    fn();
}
Run Code Online (Sandbox Code Playgroud)

我的问题是,当 T 是唯一类型时,此解决方案可以正常工作。因此传递 lambda 是可以的,但传递函数会默默地导致错误。然后我将 fn 参数包装到 lambda 中,但令人惊讶的是它没有帮助;

这是一个完整的示例:

#include <type_traits>
#include <iostream>
#include <functional>

template <typename T>
void …
Run Code Online (Sandbox Code Playgroud)

c++ lambda templates c++11

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

为什么这个显式析构函数会导致共享ptr中的内存损坏?

这段代码有什么问题,如何解决?

#include <iostream>
#include <boost/shared_ptr.hpp>
#include <vector>

struct CTest
{
    CTest()
    { std::cout << "ctor CTest" <<std::endl; }

    ~CTest()
    { std::cout << "dtor CTest" <<std::endl; }
};

struct CSlot
{
    CSlot() : m_test(new CTest()), m_num(123)
    { }

    ~CSlot()
    {
        // m_test.reset(); // this line fixed the code but I don't know why
        m_num = -1;
    }

    boost::shared_ptr<CTest> m_test;
    int m_num;
};

int main()
{
    std::vector<CSlot> testVector(1);

    std::cout << "1" << std::endl;
    new (&testVector[0]) CSlot();

    // clear slot
    testVector[0].~CSlot();
    std::cout << …
Run Code Online (Sandbox Code Playgroud)

c++ valgrind destructor explicit-constructor shared-ptr

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

为什么g ++与-O3段错误与以下LLVM库代码

所以我想使用llvm::Twine字符串块类.

我有以下样本:

#include <llvm/ADT/Twine.h>
#include <iostream>

int main()
{
  llvm::Twine twine1 = llvm::Twine("aaaa") + "bbbb" + "cccc" + "dddd";
  llvm::Twine twine2 = llvm::Twine(twine1) + "dddd" + "eeee";
  std::cout << twine1.str() << std::endl;
  std::cout << twine2.str() << std::endl;

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

它运行与clang++使用-O3,并g++-O0但段错误g++使用-O3.我尝试了这个代码从3.4-3.9部分不同版本的clang库并试过g++ 4.8.4,g++ 4.8.5mingw-5.3.0.

您需要llvm库并将代码与-lLLVMSupport -lLLVMCore其他代码链接起来llvm-config --ldflags

c++ llvm clang c++11

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