小编Dam*_*ian的帖子

使用curley brakets解析文件

我需要解析一个文件,其中包含用大括号分隔的信息,例如:

Continent
{
Name    Europe
Country
{
Name    UK
Dog
{
Name    Fiffi
Colour  Gray
}
Dog
{
Name    Smut
Colour  Black
}
}
}
Run Code Online (Sandbox Code Playgroud)

这是我在Python中尝试过的

from io import open
from pyparsing import *
import pprint

def parse(s):
    return nestedExpr('{','}').parseString(s).asList()

def test(strng):
    print strng
    try:
        cfgFile = file(strng)
        cfgData = "".join( cfgFile.readlines() )
        list = parse( cfgData )
        pp = pprint.PrettyPrinter(2)
        pp.pprint(list)

    except ParseException, err:
        print err.line
        print " "*(err.column-1) + "^"
        print err

    cfgFile.close()
    print
    return list

if __name__ …
Run Code Online (Sandbox Code Playgroud)

python parsing pyparsing

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

C#中的ObjectStore

是否存在针对csharp的ObjectStore实现,它允许为我自己的对象生成字符串句柄,稍后从字符串句柄中查找这些对象.我可以看到有C++的实现,但在dotnet中我们有一个垃圾收集器!拉尔斯

c#

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

使用 FRIEND_TEST 是正确的做法吗?

当我在https://github.com/google/googletest/blob/master/googletest/include/gtest/gtest_prod.h上查看 FRIEND_TEST 的实现时,我看到以下内容:

#ifndef GTEST_INCLUDE_GTEST_GTEST_PROD_H_
#define GTEST_INCLUDE_GTEST_GTEST_PROD_H_
// When you need to test the private or protected members of a class,
// use the FRIEND_TEST macro to declare your tests as friends of the
// class.  For example:
//
// class MyClass {
//  private:
//   void MyMethod();
//   FRIEND_TEST(MyClassTest, MyMethod);
// };
//
// class MyClassTest : public testing::Test {
//   // ...
// };
//
// TEST_F(MyClassTest, MyMethod) {
//   // Can call MyClass::MyMethod() here.
// } …
Run Code Online (Sandbox Code Playgroud)

c++ googletest

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

operator ++()nothrow不编译

为什么我不能使operator ++()nothrow?

这可能是使用postfix ++运算符(通过前缀++运算符)的少数几个优点之一.

例如,此代码无法编译

class Number
{
public:
    Number& operator++ ()     // ++ prefix
    {
        ++m_c;
        return *this;
    }

    Number operator++ (int) nothrow  // postfix ++
    {
        Number result(*this);   // make a copy for result
        ++(*this);              // Now use the prefix version to do the work
        return result;          // return the copy (the old) value.
    }

    int m_c;
};
Run Code Online (Sandbox Code Playgroud)

另外注意,后缀运算符也可以是线程安全的.

c++ nothrow

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

如何防止unique_ptr中的内存松动

下面的代码将导致内存丢失,因为rA在构造时被初始化为无效.我该怎么办才能解决这个问题?

使用shared_ptr或希望将来的编译器版本能够捕获这个错误的代码?

#include <memory>
using namespace std;

struct A {};
void use(const A& a) {};

unique_ptr<A> foo()
{
    unique_ptr<A> pa(new A());
    return pa;
}

int main()
{
    const A& rA = *foo(); // rA is unusable, initialized with invalid reference (invalidated by destruction of temporary unique_ptr returned from foo)
    use(rA);
}
Run Code Online (Sandbox Code Playgroud)

c++ smart-pointers c++11

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

源代码中的URI

任何人解释为什么我可以将URI放在C++源代码中?

#include <iostream>
int main() {
    using namespace std;
    http://www.google.com
    int x = 5;
    cout << x;
}
Run Code Online (Sandbox Code Playgroud)

这对我来说很奇怪?

例如,Visual Studio 2015给我一个警告:警告C4102:'http':未引用的标签,但代码编译!

c++

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

binary'> =':'std :: chrono :: system_clock :: time_point'

我不知道如何驾驭这个编译错误:

error C2676: binary '>=': 'std::chrono::system_clock::time_point'
Run Code Online (Sandbox Code Playgroud)
#include <ctime>
#include <chrono>

int main()
{
  std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
  std::time_t now_c = std::chrono::system_clock::to_time_t(now - std::chrono::hours(24));

  if (std::chrono::system_clock::now() >= now_c)
  {

  }
}
Run Code Online (Sandbox Code Playgroud)

以下是编译器输出的内容:

1>------ Build started: Project: test, Configuration: Debug x64 ------
1>  Source.cpp
1>d:\dev\cpptests\test\test\source.cpp(25): error C2784: 'bool std::operator >=(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)': could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'std::chrono::system_clock::time_point'
1>  c:\program files (x86)\microsoft visual studio 14.0\vc\include\utility(311): note: see declaration of 'std::operator >='
1>d:\dev\cpptests\test\test\source.cpp(25): error C2784: 'bool …
Run Code Online (Sandbox Code Playgroud)

c++ c++11 c++-chrono visual-studio-2015

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