如何从标准文件对象(如sys.stdin)或从"打开"获得的内容中创建BufferedReader对象?
(背景:我需要一个peek()方法,标准文件对象失败了.任何解决这个问题的建议也是受欢迎的.)
我有点期待这个工作,但它没有:
>>> import sys
>>> import io
>>> io.BufferedReader(sys.stdin)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'file' object has no attribute 'readable'
Run Code Online (Sandbox Code Playgroud)
(这是Python 2.7)
哈,得到它,至少对于任何有文件描述符的东西.
stream = sys.stdin, or open(...), etc.
reader = io.open(stream.fileno(), mode='rb', closefd=False)
Run Code Online (Sandbox Code Playgroud) Itanium ABI 指定,有一些无趣的异常,返回类型包含在模板实例的错位名称中,但不包含在非模板中.
为什么是这样?在什么情况下,您可以有两个函数模板实例,其中链接器需要区分它们,因为它不表示单定义规则违规或类似?
作为我的意思的一个例子:
class ReturnType {};
class ParamType {};
template <typename T>
ReturnType foo(T p) {
return ReturnType();
};
template ReturnType foo<ParamType>(ParamType);
ReturnType bar(ParamType p) {
return ReturnType();
}
Run Code Online (Sandbox Code Playgroud)
然后生成的对象文件具有重整:
ReturnType foo<ParamType>(ParamType)
=> _Z3fooI9ParamTypeE10ReturnTypeT_
^^^^^^^^^^^^
ReturnType bar(ParamType)
=> _Z3bar9ParamType
Run Code Online (Sandbox Code Playgroud)
为什么foo需要ReturnType损坏但bar不是?
(我假设有一个原因,这不仅仅是一个随意的选择.)
我在一些生产代码中遇到问题,我将其最小化到以下测试用例:
template<typename T>
void intermediate(T t)
{
func(t); // line 4 ("func not declared in this scope")
}
namespace ns {
struct type {};
}
void func(ns::type const & p); // line 11 ("declared here, later")
void foo(ns::type exit_node)
{
intermediate(exit_node); // line 15 ("required from here")
}
Run Code Online (Sandbox Code Playgroud)
GCC 4.5汇编了这个罚款.有和没有-std=c++11,4.7和4.9产生如下消息:
test.cpp: In instantiation of ‘void intermediate(T) [with T = ns::type]’:
test.cpp:15:27: required from here
test.cpp:4:5: error: ‘func’ was not declared in this scope, and no declarations were found …Run Code Online (Sandbox Code Playgroud) 我正在使用C++ 11的<regex>支持,并想检查字符串的开头是否与正则表达式匹配.[如果有帮助我可以切换到Boost,但我的印象是它们基本相同.]
显然,如果我能控制表达式的实际文本表示,我可以^在它的开头作为锚点.
但是,如果我只有一个regex(或basic_regex)对象怎么办?我可以修改它代表的正则表达式来添加锚吗?或者我必须使用regex_search,得到结果,并检查它是否从0位开始?
我最近花了相当多的时间来追踪一个问题,原因是编译了一个库-D_GLIBCXX_DEBUG(告诉libstdc ++使用带有额外检查的标准库的调试版本),但没有编译客户端程序.这导致了ABI兼容性问题.
有什么方法可以通过GCC自动检测这样的问题吗?Visual Studio提供了我认为可以用于此目的的detect_mismatch编译指示,但我不知道任何GCC等价物.GCC通过嵌入符号名称(例如GLIBCXX_3.4.9)做了一些事情,我可以想象如果相应的符号(例如mylib_debug_stl)不存在则会因为未定义的符号而导致链接错误的方案,但是我能想到的唯一方法是使用那个符号真是太烂了.
或者,其他人如何避免这个问题?将库的已检查版本构建为不同的名称或类似的名称?