以下代码是否安全地使用自定义消息抛出异常?
#include <exception>
#include <sstream>
#include <string>
#include <iostream>
int main() {
try {
std::ostringstream msg;
msg << "give me " << 5;
throw std::exception(msg.str().c_str());
} catch (std::exception& e) {
std::cout << "exception: " << e.what();
}
}
Run Code Online (Sandbox Code Playgroud)
使用VC++ - 2008,这给出了:
exception: give me 5
Run Code Online (Sandbox Code Playgroud)
但现在我想知道为什么来自本地对象的消息"给我5" msg
仍然可以在catch块中找到?在打印消息时,应该删除流和临时字符串对象吗?顺便说一句:这种为异常生成消息的方式似乎也适用于多个函数,如果在打印异常之前在catch块中分配了新内存.
或者是否需要使用std :: string成员定义自定义异常类,以便安全地保留消息直到打印它.
不知怎的,我喜欢这些"最短"的程序,显示出(基本的?)问题.在VS2008中测试一些模板代码时出现此错误(VS2010和VS2012也已确认,见下文):
c:\ program files(x86)\ microsoft visual studio 9.0\vc\include\xmemory(225):错误C2752:'std :: _ Ptr_cat_helper <_T1,_T2>':多个部分特化匹配模板参数列表
Run Code Online (Sandbox Code Playgroud)with [ _T1=const float (**), _T2=const float (**) ]
我可以将问题归结为以下三行:
#include <vector>
typedef float TPoint[3];
std::vector<TPoint const*> points; // error C2752
Run Code Online (Sandbox Code Playgroud)
请注意,以下都可以
#include <vector>
#include <list>
typedef float TPoint[3];
// these similar usages of TPoint are all ok:
std::vector<TPoint*> points; // no error
TPoint const* points1[2];
std::list<TPoint const*> points2;
Run Code Online (Sandbox Code Playgroud)
我试图通过为struct _Ptr_cat_helper提供额外的模板特性来修复xutility - 没有运气.任何想法出了什么问题?或者如何在不失去的情况下解决const
?
使用 C 字符串,我将编写以下代码来从文件路径获取文件名:
#include <string.h>
const char* filePath = "dir1\\dir2\\filename"; // example
// extract file name (including extension)
const char* fileName = strrchr(progPath, '\\');
if (fileName)
++fileName;
else
fileName = filePath;
Run Code Online (Sandbox Code Playgroud)
如何对 C++ 字符串执行同样的操作?(即使用std::string
from #include <string>
)
我必须找到以下哪个值可以是具有6个顶点的无向图的度数:
a)3 2 2 2 3 3
b)4 2 2 2 3 2
c)5 2 2 2 0 3
d)5 2 2 2 1 2
我发现的唯一方法是尝试在一张纸上绘制图形,然后检查是否可行.我只需要一个提示来启动这个问题,如果可能的话,除了绘制每个图之外.
我想标记我自己的SQL语法扩展.这涉及识别双引号字符串中的转义双引号.例如在MySQL中,这两个字符串标记是等价的:( """"
第二个双引号充当转义字符)和'"'
.我尝试了不同的东西,但我仍然坚持如何替换令牌的价值.
#include <boost/spirit/include/lex_lexertl.hpp>
namespace lex = boost::spirit::lex;
template <typename Lexer>
struct sql_tokens : lex::lexer<Lexer>
{
sql_tokens()
{
string_quote_double = "\\\""; // '"'
this->self("INITIAL")
= string_quote_double [ lex::_state = "STRING_DOUBLE" ] // how to also ignore + ctx.more()?
| ...
;
this->self("STRING_DOUBLE")
= lex::token_def<>("[^\\\"]*") // action: ignore + ctx.more()
| lex::token_def<>("\\\"\\\"") // how to set token value to '"' ?
| lex::token_def<>("\\\"") [ lex::_state = "INITIAL" ]
;
}
lex::token_def<> string_quote_double, ...;
};
Run Code Online (Sandbox Code Playgroud)
那么如何将令牌的值设置为何"
时"" …
考虑以下用于清理目录的 python 函数:
def cleanDir(path):
shutil.rmtree(path)
os.mkdir(path)
Run Code Online (Sandbox Code Playgroud)
在 Windows 上(实际在 Windows7 和 Windows10 上使用 python 2.7.10 和 3.4.4 进行测试),当使用 Windows 资源管理器同时导航到相应目录时(或者仅在左侧树窗格中导航到父文件夹时),可能会引发以下异常:
Traceback (most recent call last):
...
File "cleanDir.py", line ..., in cleanDir
os.mkdir(path)
PermissionError: [WinError 5] Access is denied: 'testFolder'
Run Code Online (Sandbox Code Playgroud)
该问题已在本期报告过。但没有进一步分析,并且给出的使用 sleep 的解决方案并不令人满意。根据下面 Eryk 的评论,当前的 python 版本(即 python 3.8)也会出现相同的行为。
注意,shutil.rmtree
返回无异常。但尝试立即再次创建该目录可能会失败。(重试大多数情况下都会成功,请参阅下面的完整测试代码。)并请注意,您需要在 Windows 资源管理器中的测试文件夹的左侧和右侧单击,以强制解决问题。
问题似乎出在 Windows 文件系统 API 函数中(而不是在 Pythonos
模块中):当 Windows 资源管理器具有相应文件夹的句柄时,已删除的文件夹似乎不会立即“转发”到所有函数。
import os, shutil
import time
def populateFolder(path):
if os.path.exists(path):
with open(os.path.join(path,'somefile.txt'), 'w') as …
Run Code Online (Sandbox Code Playgroud) c++ ×4
visual-c++ ×2
algorithm ×1
boost-spirit ×1
exception ×1
graph ×1
python ×1
std ×1
stl ×1
windows ×1