AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / user-1715765

H.v.M.'s questions

Martin Hope
H.v.M.
Asked: 2025-02-05 14:26:07 +0800 CST

为什么我不能分配 std::lock_guard 的 std::optional?

  • 5

为什么我不能声明一个可选项std::lock_guard,然后稍后再分配它?对于可选字符串,同样的事情也可以正常工作。

这有效:

std::mutex m;
std::optional<std::lock_guard<std::mutex>> lg(m);

但这不行:(*)

std::mutex m;
std::optional<std::lock_guard<std::mutex>> lg;
lg = std::lock_guard<std::mutex>(m);

但事实并非如此:(**)

std::mutex m;
std::optional<std::lock_guard<std::mutex>> lg;
lg = std::optional<std::lock_guard<std::mutex>>(m);

但这确实:

std::string s;
std::optional<std::string> os;
os = std::string(s);

事实也是如此:

std::string s;
std::optional<std::string> os;
os = std::optional<std::string>(s);

(*) 错误信息:

1>c:\dev\repos\tp_iu2\iu2\pretests\j00010_adaptercal.cpp(51): error C2679: binary '=': no operator found which takes a right-hand operand of type 'std::lock_guard<std::mutex>' (or there is no acceptable conversion)
1>c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.13.26128\include\optional(547): note: could be 'std::optional<std::lock_guard<std::mutex>> &std::optional<std::lock_guard<std::mutex>>::operator =(const std::optional<std::lock_guard<std::mutex>> &)'
1>c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.13.26128\include\optional(320): note: or       'std::optional<std::lock_guard<std::mutex>> &std::optional<std::lock_guard<std::mutex>>::operator =(std::nullopt_t) noexcept'
1>c:\dev\repos\tp_iu2\iu2\pretests\j00010_adaptercal.cpp(51): note: while trying to match the argument list '(std::optional<std::lock_guard<std::mutex>>, std::lock_guard<std::mutex>)'

(**) 错误信息:

1>c:\dev\repos\tp_iu2\iu2\pretests\j00010_adaptercal.cpp(51): error C2280: 'std::optional<std::lock_guard<std::mutex>> &std::optional<std::lock_guard<std::mutex>>::operator =(const std::optional<std::lock_guard<std::mutex>> &)': attempting to reference a deleted function
1>c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.13.26128\include\optional(547): note: compiler has generated 'std::optional<std::lock_guard<std::mutex>>::operator =' here
1>c:\program files (x86)\microsoft visual studio\2017\professional\vc\tools\msvc\14.13.26128\include\optional(547): note: 'std::optional<std::lock_guard<std::mutex>> &std::optional<std::lock_guard<std::mutex>>::operator =(const std::optional<std::lock_guard<std::mutex>> &)': function was implicitly deleted because a base class invokes a deleted or inaccessible function 'std::_Deleted_move_assign<_Base,_Ty> &std::_Deleted_move_assign<_Base,_Ty>::operator =(const std::_Deleted_move_assign<_Base,_Ty> &)'

解决方案是使用std::unique_ptr<std::lock_guard<std::mutex>>,所以我只是出于好奇才问。

c++
  • 2 个回答
  • 120 Views
Martin Hope
H.v.M.
Asked: 2024-12-22 07:57:10 +0800 CST

使用 SETLOCAL ENABLEDELAYEDEXPANSION 的批处理脚本中带有“!”后跟“:”的字符串

  • 6

我有一个使用和接受用户输入的脚本。如果输入字符串包含和,SETLOCAL ENABLEDELAYEDEXPANSION则会出现问题,因为这些字符及其之间的所有内容都会被删除。!:

示例:我将a!b:c用户输入输入到x并将其直接保存到脚本中作为y和z。

SETLOCAL ENABLEDELAYEDEXPANSION
SET x=
SET /P x=
SET y=a!b:c
SET "z=a!b:c"
echo %x% %y% %z%

结果是ac ac ac。对于直接输入的字符串(如y和z),我该如何修复这个问题?对于用户输入的字符串(如),我该如何修复这个问题x?

batch-file
  • 2 个回答
  • 51 Views
Martin Hope
H.v.M.
Asked: 2024-04-25 01:27:18 +0800 CST

在函数签名中声明和命名类型,以便可以在函数中重复使用它

  • 5

我有一个实用函数,用于根据函子转换可迭代对象:

template<typename Iterable, typename Functor>
auto transform(Iterable const& input, Functor&& transformer) -> std::vector<decltype(transformer(*input.cbegin()))>
{
    std::vector<decltype(transformer(*input.cbegin()))> output;
    std::for_each(
        std::cbegin(input),
        std::cend(input),
        [&output, &transformer](auto const& m) { output.emplace_back(transformer(m)); });
    return output;
}

void test_transform()
{
    std::vector<int> ints { 10, 20, 30 };
    auto transformer = [](int i) { return std::to_string(i + 1); };
    std::vector<std::string> strs = VectorUtil::transform(ints, transformer);
    for (std::string s : strs) { std::cout << s << std::endl; }
}

必须将返回类型写两次是相当难看的。有没有办法给其中一个命名:

decltype(transformer(*input.cbegin()))

或者

std::vector<decltype(transformer(*input.cbegin()))>

那么它可以重复使用吗?

c++
  • 2 个回答
  • 44 Views
Martin Hope
H.v.M.
Asked: 2024-02-29 19:45:23 +0800 CST

如何让 Git 命令不等待我点击“q”?

  • 5

如果我输入git log; git push,我将得到一个很长的日志输出,在执行以下命令之前我必须通过按“q”退出。git status不这样做;如果我更改了一千个文件,它仍然会打印整个输出而无需等待。

如何使我的 Git 命令(例如git log或 )git config --list像那样打印所有内容git status?

git
  • 2 个回答
  • 57 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    重新格式化数字,在固定位置插入分隔符

    • 6 个回答
  • Marko Smith

    为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会?

    • 2 个回答
  • Marko Smith

    VScode 自动卸载扩展的问题(Material 主题)

    • 2 个回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Martin Hope
    Fantastic Mr Fox msvc std::vector 实现中仅不接受可复制类型 2025-04-23 06:40:49 +0800 CST
  • Martin Hope
    Howard Hinnant 使用 chrono 查找下一个工作日 2025-04-21 08:30:25 +0800 CST
  • Martin Hope
    Fedor 构造函数的成员初始化程序可以包含另一个成员的初始化吗? 2025-04-15 01:01:44 +0800 CST
  • Martin Hope
    Petr Filipský 为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会? 2025-03-23 21:39:40 +0800 CST
  • Martin Hope
    Catskul C++20 是否进行了更改,允许从已知绑定数组“type(&)[N]”转换为未知绑定数组“type(&)[]”? 2025-03-04 06:57:53 +0800 CST
  • Martin Hope
    Stefan Pochmann 为什么 {2,3,10} 和 {x,3,10} (x=2) 的顺序不同? 2025-01-13 23:24:07 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve