考虑以下简单的c ++程序
#include <iostream>
#include <regex>
int main(int argc, char * argv[])
{
std::regex foobar( "[A]+");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
使用-fpack-struct = 1进行编译时会出现故障
g++-5 -std=gnu++14 ./fpack_regex.cpp -fpack-struct=1 -o a.out && a.out
Segmentation fault (core dumped)
Run Code Online (Sandbox Code Playgroud)
而
g++-5 -std=gnu++14 ./fpack_regex.cpp -o a.out && a.out
Run Code Online (Sandbox Code Playgroud)
工作得很好.
任何线索为什么pack-struct = 1选项可能导致此失败?
我正在使用pyparsing来构建附加到列表的字典.当我这样做时,字典被包含在一个额外的列表中,并且还附加了一个空的字典.我不知道如何解决这个问题.我想要的是[{},{},{}].我得到了[([{}],{})]为什么getDict的代码给了我想要的东西而不是getDictParse?
#! /usr/bin/env python
from pyparsing import Literal, NotAny, Word, printables, Optional, Each, Combine, delimitedList, printables, alphanums, nums, White, OneOrMore, Group
noParseList = []
parseList = []
def getDict():
return {'duck':'moose','cow':'ewe'}
def getDictParse(str, loc, toks):
return {'duck2':toks[0],'cow2':'ewe'}
parser = Word(alphanums)
parser.setParseAction(getDictParse)
parseList.append(parser.parseString("monkey"))
noParseList.append(getDict())
print noParseList
print parseList
Run Code Online (Sandbox Code Playgroud)
输出:
[{'cow': 'ewe', 'duck': 'moose'}]
[([{'cow2': 'ewe', 'duck2': 'monkey'}], {})]
Run Code Online (Sandbox Code Playgroud) 我有一个模板类Foo,它是T类型.当我创建一个TI实例时,要确保构造函数传递相同的类型.
除了隐式转换的细节之外,编译器确保了这一点.我想阻止这些,我无法弄清楚是否有一个好方法.编译器标志不是这里的选项.
我实际上是在试图阻止从double到float的隐式转换,因为我的Foo类正在做一些有趣的魔法,炸掉了所谓的演员阵容.有什么建议?
template <typename T>
class Foo {
public:
explicit Foo(const T& x) {kBitCount = sizeof(T); }
size_t kBitCount;
size_t mySize(){ return kBitCount; } // Size used to demonstrate
};
int main(int argc, char *argv[])
{
short sh = 5;
Foo<int> foo_int_from_short(sh); // I want this to fail
std::cout << "size:" << foo_int_from_short.mySize() << std::endl; // prints 4
Foo<short> foo_sh((unsigned int)5); // I want this to fail
std::cout << "size:" << foo_sh.mySize() << std::endl; // Prints 2
return 0; …Run Code Online (Sandbox Code Playgroud)