在 C++20 中,我们可以分配内存constexpr只要在上下文 \xe2\x80\x94 中释放内存,我们就可以在上下文中分配内存,即这是有效的:
constexpr int* g(){\n int* p = new int(100);\n return p;\n}\n\nconstexpr int f(){\n int* ret = g();\n int i = *ret;\n delete ret;\n return i;\n}\nstatic_assert(f() == 100);\nRun Code Online (Sandbox Code Playgroud)\n然而这不会编译:
\nconstexpr int* g(){\n int* p = new int(100);\n return p;\n}\n\nconstexpr int f(){\n int* ret = g();\n int i = *ret;\n return i;\n}\nstatic_assert(f() == 100);\nRun Code Online (Sandbox Code Playgroud)\n出现错误:
\nerror: '(f() == 100)' is not a constant expression because allocated storage has not been deallocated\n …Run Code Online (Sandbox Code Playgroud) C++ 不允许 constexpr 内联汇编有充分的理由吗?为什么 C++20 中允许未计算的内联汇编表达式?
是否可以在 中C++重载""_something函数标识符或可调用函数的运算符以使其具有自定义行为?
我最近在这个 cppcon 视频中看到了类似的内容,其中演示者展示了如何使用模块、零宏构建单元测试框架......但我不太明白这是如何""_test可能的,或者如何C++理解该可调用应该执行这种操作定义在运算符重载实现的主体中。
template <typename T>
auto "some_name"_test(T a, T b);
Run Code Online (Sandbox Code Playgroud)
有人可以解释一下这背后的细节吗?
我遇到了以下代码:
auto x = new int[10][10];
Run Code Online (Sandbox Code Playgroud)
它可以正确编译和运行,但我无法弄清楚与x赋值分开定义的类型是什么。
调试时显示的类型是int(*)[10]但x(int (*) x[10];或我尝试过的任何其他组合)是非法的。
那么是否存在auto无法用显式类型替换的情况......?(这是这样的情况吗?)
请考虑以下简单的代码片段:
template<typename T>
struct point2{ T x, y; };
template<typename T>
std::complex<T> foo(std::vector<point2<T>> const& x)
{
std::reduce(std::execution::par_unseq, x.begin(), x.end(), std::complex<T>{},
[&](std::complex<T> const& first, point2<T> const& second) {
return first + std::complex<T>{ second.x, second.y };
});
}
Run Code Online (Sandbox Code Playgroud)
使用 Visual Studio 2022(C++ 语言设置为 C++20)时,我收到错误消息
'int foo::<lambda_1>::operator ()(const std::complex &,const point2 &) const': 无法将参数 2 从 'std::complex' 转换为 'const point2 &'
这里出了什么问题?定义中的迭代器指向的类型似乎std::reduce需要与所选的初始值相同。但这似乎不是 C++ 标准中描述的要求。我错过了什么吗?
我有一个类模板,我想编写如下:
template </*what to put here?*/ T>
Class Bar {};
Run Code Online (Sandbox Code Playgroud)
我想强制 T 只能是作用域枚举中的值。我使用了此处is_scoped_enum提供的类型检查,但是我能想到的最好的办法就是更改为如下所示:Bar
template <typename T>
concept ScopeEnum = is_scoped_enum<T>::value;
template<ScopeEnum SE, SE se>
class Bar {};
Run Code Online (Sandbox Code Playgroud)
我怎样才能实现它并Bar保持预期的效果?
下面简单的代码。
class Base{
public:
int fcn();
};
int main() {
Base b; // clause 1
}
Run Code Online (Sandbox Code Playgroud)
为什么会这样编译?第 1 条创建了一个实例,但从未定义Base该函数。fcn()
在下面的代码中,我尝试获取派生自 的自定义类型的元组大小std::tuple。但编译器抱怨这std::tuple_size是不完整的......我无法真正理解,因为struct foo此时已经明确定义了。自然也应该如此type_descriptor<foo>。这个错误是从哪里来的?
#include <utility>
#include <tuple>
#include <cstdio>
struct foo
{
int a_;
int b_;
};
template <typename T>
struct type_descriptor
{};
template<>
struct type_descriptor<foo>
: std::tuple<int, bool>
{
};
int main(){
printf("Size of tuple = %zu\n", std::tuple_size_v<type_descriptor<foo>>);
}
Run Code Online (Sandbox Code Playgroud)
这会产生以下错误:
<source>:89:27: required from here
/opt/compiler-explorer/gcc-12.2.0/include/c++/12.2.0/bits/utility.h:75:61: error: incomplete type 'std::tuple_size<type_declarator<foo> >' used in nested name specifier
75 | inline constexpr size_t tuple_size_v = tuple_size<_Tp>::value;
|
Run Code Online (Sandbox Code Playgroud) 使用现代 C++,拥有由一个线程初始化的共享内存(第一个到达这一点,然后由多个线程读取)的最佳方法是什么?它需要尽可能轻。
int *ptr = nullptr;
void parallel_work() {
// this should only done by the first thread to this point
ptr = init_ptr();
// all threads read After ptr is set
do_with(ptr);
}
int main() {
std::thread th0 { ¶llel_work };
std::thread th1 { ¶llel_work };
std::thread th2 { ¶llel_work };
parallel_work();
}
Run Code Online (Sandbox Code Playgroud)
如果它可以帮助的话,我真的想避免将代码的整个读取部分包装在mutex.
PS:这不是static函数变量的用例,因为我将在程序的生命周期中创建其中的许多变量。
我通过 Stanley 的《C++ Primer》一书了解了 C++ 中的类模板。然后我编写了以下程序,令人惊讶的是,它可以使用 gcc 编译,但不能使用 clang 编译。我不知道为什么会这样。也就是说,C++20 中哪个编译器是正确的。我还阅读了有关未定义行为的信息,但我不确定该程序是否具有该行为。
template <typename T>
struct test
{
T y;
};
int main()
{
test t{1}; //compiles with gcc and msvc but not with clang!
}
Run Code Online (Sandbox Code Playgroud)
那么有人可以告诉我根据 C++20 标准什么是正确的行为吗?