小编Roi*_*ton的帖子

将unique_ptr移至lambda时,为什么无法调用reset?

当移动std::unique_ptr到拉姆达,这是不可能的调用reset()就可以了,因为它似乎是常量,则:

error C2662: void std::unique_ptr<int,std::default_delete<_Ty>>::reset(int *) noexcept': cannot convert 'this' pointer from 'const std::unique_ptr<int,std::default_delete<_Ty>>' to 'std::unique_ptr<int,std::default_delete<_Ty>> &
Run Code Online (Sandbox Code Playgroud)
#include <memory>
int main()
{
    auto u = std::unique_ptr<int>();
    auto l = [v = std::move(u)]{
        v.reset(); // this doesn't compile
    };
}
Run Code Online (Sandbox Code Playgroud)
  1. 为什么会这样?
  2. 是否可以通过std::unique_ptr允许reset()在lambda(使用C ++ 17或更高版本)中调用的另一种方式捕获?

c++ lambda unique-ptr c++11 capture-list

34
推荐指数
4
解决办法
1632
查看次数

C++ 17 std :: filesystem :: path中的本机路径分隔符错误?

从升级当我遇到一个问题#include <experimental/filesystem>#include <filesystem>.似乎该std::filesystem::path::wstring方法没有返回与中相同的字符串experimental::filesystem.我编写了以下小测试程序,其中包含输出结果.

#include <iostream>
#include <filesystem>
#include <experimental/filesystem>

namespace fs = std::filesystem;
namespace ex = std::experimental::filesystem;
using namespace std;

int main()
{
    fs::path p1{ L"C:\\temp/foo" };    
    wcout << "std::filesystem Native: " << p1.wstring() << "  Generic: " << p1.generic_wstring() << endl;

    ex::path p2{ L"C:\\temp/foo" };
    wcout << "std::experimental::filesystem Native: " << p2.wstring() << "  Generic: " << p2.generic_wstring() << endl;
}

/* Output:
std::filesystem Native: C:\temp/foo  Generic: C:/temp/foo
std::experimental::filesystem Native: C:\temp\foo …
Run Code Online (Sandbox Code Playgroud)

c++ c++17 std-filesystem

18
推荐指数
3
解决办法
2046
查看次数

修剪字符串中特定的前导和尾随字符

我有一个包含路径的字符串

str = "/example/path/with/different/trailing/delimiter\"
Run Code Online (Sandbox Code Playgroud)

我要修剪开头和结尾/\。Python 3的最佳做法是什么?

目前我正在使用

trimmedPath = str.strip("/\\")
# trimmedPath is "example/path/with/different/trailing/delimiter" as desired
Run Code Online (Sandbox Code Playgroud)

两个问题:

  1. 这是用于修剪Python 3中特定字符的最佳修剪功能吗?
  2. Python 3中是否有针对此类操作的特定路径函数,所以我不必手动设置定界符?

python python-3.x

15
推荐指数
2
解决办法
3万
查看次数

将字符串转换为std文件系统路径

文件路径作为字符串传递.如何将此字符串转换为std :: filesystem :: path?例:

#include <filesystem>

std::string inputPath = "a/custom/path.ext";
const std::filesystem::path path = inputPath; // Is this assignment safe?
Run Code Online (Sandbox Code Playgroud)

c++ c++17

8
推荐指数
2
解决办法
5534
查看次数

在Visual Studio 2017中使用OpenMP 3/4

尝试使用OpenMP 3中的功能

#pragma omp parallel for collapse(2)
Run Code Online (Sandbox Code Playgroud)

在Visual Studio 2017中; 我越来越error c3005: 'collapse' unexpected token encountered on openmp 'parallel for' directive

Visual Studio 2017似乎只支持OpenMP2.在一个支持OpenMP4.5请求中,来自VS团队的说法

我们目前没有计划.

另一个答案说

幸运的是,clang-cl已成为OpenMP 4支持的可行替代方案.在最坏的情况下,您仍然可以启用/ fallback选项.

如何在Visual Studio 2017中使用clang-cl以及什么是后备选项?

c++ openmp clang-cl visual-studio-2017

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

如何递归复制文件和目录

使用C++,是否可以递归地将文件和目录从一个路径复制到另一个路径

  • 无需使用任何额外的库?
  • 并具有平台独立功能?

考虑以下文件系统

src/fileInRoot
src/sub_directory/
src/sub_directory/fileInSubdir
Run Code Online (Sandbox Code Playgroud)

我要复制

  1. 所有文件和目录或
  2. 某些文件和目录

src另一个目录target.


我创建了一个新问题,因为我发现的问题是特定于平台的,不包括过滤:

c++ filesystems cross-platform

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

为什么Visual C++版本号中包含逗号而不是点

我已经看到我的Visual C++项目具有以下声明,这些声明使用COMMAS而不是DOTS用于版本:

#define FILEVER        11,0,2,0
#define PRODUCTVER     11,0,2,0
#define STRFILEVER     "11, 0, 2, 0\0"
#define STRPRODUCTVER  "11, 0, 2, 0\0"
Run Code Online (Sandbox Code Playgroud)

这里MS文章也有逗号相同的值(实际上上述声明基于该文章).我们为什么在这里使用COMMAS?当我打开编译的文件属性时,我看到FileVersion为11.0.2.0但是ProductVersion为11,0,2,0 - 我的QA朋友说它是一个bug :).我缺少一些标准或者一些内部机制吗?

versioning version-numbering visual-c++

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

如何从字符串变量创建const字符串

我想从另一个字符串变量创建const字符串.例如,接下来的两个代码片段无法编译

1)

string str = "111";
const string str2 = str;
Run Code Online (Sandbox Code Playgroud)

2)

string str = "111";
const string str2 = new string(str.ToCharArray());
Run Code Online (Sandbox Code Playgroud)

结果如何

Error: The expression being assigned to 'str2' must be constant 
Run Code Online (Sandbox Code Playgroud)

有没有办法从字符串变量创建一个const字符串?

c# string const

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

ExprTk:表达式的值在更改时是否必须重新编译

我正在使用exprtk创建一个表达式,使用不断变化的变量.

每次更改变量的值时,是否必须重置并重新编译exprtk::expression使用更新exprtk::symbol_table

或者更新的值是否由现有的编译表达式直接评估?

#include <iostream> 
#include <string>
#include "exprtk.hpp"

int main() {
    std::string expression_string = "y := x + 1";

    int x = 1;

    exprtk::symbol_table<int> symbol_table;
    symbol_table.add_variable("x", x);

    exprtk::expression<int> expression;
    expression.register_symbol_table(symbol_table);

    exprtk::parser<int> parser;

    if (!parser.compile(expression_string, expression))
    {
        std::cout << "Compilation error." << std::endl;
        return 1;
    }

    expression.value(); // 1 + 1

    x = 2;
    // Do I have to create a new symbol_table, expression and parse again?

    // Or does the expression evaluate the …
Run Code Online (Sandbox Code Playgroud)

c++ exprtk

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

如何在 C++17 中检索 std::filesystem::file_time_type 的时钟类型

由于在 C++17 中std::filesystem::file_time_type使用了一个普通时钟,有没有办法检索file_time_typeC++17**的实际时钟类型?

目标是将时间转换为std::chrono::system_clock在流中使用它。


** 在 C++20 中,file_time_type将使用std::chrono::file_clock,它具有operator<<并且可以使用std::chrono::clock_cast.

c++ c++-chrono c++17 std-filesystem

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