小编cop*_*roc的帖子

std :: exception使用来自本地对象的消息

以下代码是否安全地使用自定义消息抛出异常?

#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成员定义自定义异常类,以便安全地保留消息直到打印它.

c++ exception visual-c++

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

STL中的VS编译器错误C2752("多个部分特化匹配")

不知怎的,我喜欢这些"最短"的程序,显示出(基本的?)问题.在VS2008中测试一些模板代码时出现此错误(VS2010和VS2012也已确认,见下文):

c:\ program files(x86)\ microsoft visual studio 9.0\vc\include\xmemory(225):错误C2752:'std :: _ Ptr_cat_helper <_T1,_T2>':多个部分特化匹配模板参数列表

   with
   [
       _T1=const float (**),
       _T2=const float (**)
   ]
Run Code Online (Sandbox Code Playgroud)

我可以将问题归结为以下三行:

#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++ stl visual-c++

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

strrchr 的 C++ 字符串等效项

使用 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::stringfrom #include <string>

c++ std

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

从给定的节点度构造图形

我必须找到以下哪个值可以是具有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

我发现的唯一方法是尝试在一张纸上绘制图形,然后检查是否可行.我只需要一个提示来启动这个问题,如果可能的话,除了绘制每个图之外.

algorithm graph

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

如何摆脱带有spirit :: lex的令牌中的转义字符?

我想标记我自己的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)

那么如何将令牌的值设置为何""" …

c++ boost-spirit boost-spirit-lex

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

在 Windows 上成功使用 shutdown.rmtree 后,os.mkdir 可能会失败并出现 PermissionError

考虑以下用于清理目录的 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)

python windows

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