从 C++20 开始,std::vector可以在常量表达式中使用。据我所知,当前的 C++ 允许动态分配内存,条件是任何此类分配在常量表达式“结束”时都被释放。
但是,我遇到的情况是,在立即运行的情况下,规则可能会有所不同。请考虑以下示例:
consteval auto getVec() {
return std::vector<int>(9);
}
static_assert( getVec().size() == 9 );
Run Code Online (Sandbox Code Playgroud)
这里立即consteval函数getVec返回 not-empty std::vector,其大小在常量表达式中验证。
我期望这段代码能够编译,因为所有释放都必须自动完成,并且实际上它在 Clang 中被接受libc++。
但 MSVC 抱怨道:
error C7595: 'getVec': call to immediate function is not a constant expression
note: (sub-)object points to memory which was heap allocated during constant evaluation
fatal error C1903: unable to recover from previous error(s); stopping compilation
Run Code Online (Sandbox Code Playgroud)
GCC 的行为类似:
error: 'getVec()()' is not a constant expression …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 C++20 中的模块。我自己的模块一切正常;我可以正常创建和使用它们。
但是,如果我想导入标准模块(std.core、std.regex 等),我会收到以下错误,并且在互联网上找不到类似的内容:
E3373 IFC 文件“C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.31.31103\ifc\x64/Debug/std.core.ifc”具有不受支持的版本 0.41
; WITH Hierarchy as
(
select distinct PersonnelNumber
, Email
, ManagerEmail
from dimstage
union all
select e.PersonnelNumber
, e.Email
, e.ManagerEmail
from dimstage e
join Hierarchy as h on e.Email = h.ManagerEmail
)
select * from Hierarchy
Run Code Online (Sandbox Code Playgroud)
你能帮助在 SPARK SQL 中实现同样的目标吗
根据文档,他们已使用 Amazon Linux 2 平台版本上的配置删除了 Gzip压缩选项。
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream fo("output/folder1/data/today/log.txt");
fo << "Hello world\n";
fo.close();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我需要将一些日志数据输出到一些具有变量名称的文件中。但是,ofstream不会一路创建目录,如果文件的路径不存在,则不会ofstream写入任何地方!
如何沿着文件路径自动创建文件夹?系统只有Ubuntu。
有人可以解释为什么插入到std::unordered_map容器中只会使迭代器无效,而不会使引用和指针无效。另外,我无法理解 https://en.cppreference.com/w/cpp/container/unordered_map/insert 中的以下声明的含义
如果插入成功,则在元素保存在节点句柄中时获得的指向该元素的指针和引用将失效,而在提取该元素之前获得的指向该元素的指针和引用将变得有效。
我想使用 来强制执行关于类静态成员变量数组长度的编译时约束static_assert()。但是,命名变量的长度lpfilter[]在标头的声明中未知,只有在稍后的定义中才知道。我不确定如何最好地实现它,static_assert以便它可以看到静态私有数组长度。我有一个解决方法,使用私有作用域函数来保存static_asserts,但是有更好的方法吗?我在 C++14 模式下使用 clang 15。
//in header
class test
{
private:
//member constants
static const float lpfilter[];
//feels like a workaround
void testChecker(void);
};
Run Code Online (Sandbox Code Playgroud)
//early in cpp file
const float test::lpfilter[] = {
#include "a_single_line_lpfilter.csv"
};
Run Code Online (Sandbox Code Playgroud)
到目前为止我已经做了一些尝试。每个构建错误都是有意义的:
//after lpfilter[] definition in cpp file
//error: 'lpfilter' is a private member of 'test'
static_assert((sizeof(test::lpfilter)/sizeof(test::lpfilter[0])) % 2, "filter length must be even");
//error: redefinition of 'test'
class test
{
static_assert((sizeof(test::lpfilter)/sizeof(test::lpfilter[0])) % 2, "filter length …Run Code Online (Sandbox Code Playgroud) 下面的代码编译正常(参见下面的 Golbolt 链接):
#include <memory>
struct B;
struct A1 {
A1() = default;
~A1();
std::unique_ptr<B> ptr;
};
#if 0
struct A2 {
A2();
~A2();
std::unique_ptr<B> ptr;
};
A2::A2() = default;
#endif
int main()
{
}
Run Code Online (Sandbox Code Playgroud)
但如果我用 替换#if 0来#if 1编译类,A2我会从 gcc 收到以下错误:
In file included from /opt/compiler-explorer/gcc-12.2.0/include/c++/12.2.0/memory:76,
from <source>:1:
/opt/compiler-explorer/gcc-12.2.0/include/c++/12.2.0/bits/unique_ptr.h: In instantiation of 'void std::default_delete<_Tp>::operator()(_Tp*) const [with _Tp = B]':
/opt/compiler-explorer/gcc-12.2.0/include/c++/12.2.0/bits/unique_ptr.h:396:17: required from 'std::unique_ptr<_Tp, _Dp>::~unique_ptr() [with _Tp = B; _Dp = std::default_delete<B>]'
<source>:17:1: required from …Run Code Online (Sandbox Code Playgroud) 我刚刚发现 libcu++ 库并尝试使用这些cuda::atomic变量。我编写了以下程序,但它给了我意想不到的结果:
#include <atomic>
#include <cuda/atomic>
#include <stdio.h>
#define gpuErrchk(ans) { gpuAssert((ans), __FILE__, __LINE__); }
inline void gpuAssert(cudaError_t code, const char *file, int line, bool abort=true)
{
if (code != cudaSuccess)
{
fprintf(stderr,"GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line);
if (abort) exit(code);
}
}
__global__ void atomic_test()
{
cuda::atomic<int, cuda::thread_scope_block> x{0};
x.fetch_add(1, cuda::memory_order_seq_cst);
__syncthreads();
int y = x.load(cuda::memory_order_acquire);
printf("(%d %d) - Value of x is %d\n", blockIdx.x, threadIdx.x, y);
}
int main()
{
atomic_test<<<2, 32>>>();
gpuErrchk( cudaDeviceSynchronize() …Run Code Online (Sandbox Code Playgroud) 下面的代码允许您查看被删除的变量的大小:
#include <iostream>
#include <stdlib.h>
using namespace std;
struct P {
static void operator delete(void* ptr, std::size_t sz)
{
cout << "custom delete for size " << sz <<endl;
delete (ptr); // ::operator delete(ptr) can also be used
}
static void operator delete[](void* ptr, std::size_t sz)
{
cout << "custom delete for size " << sz <<endl;
delete (ptr); // ::operator delete(ptr) can also be used
}
};
int main()
{
P* var1 = new P;
delete var1;
P* var2 …Run Code Online (Sandbox Code Playgroud) c++ overloading operator-overloading dynamic-memory-allocation
c++ ×7
c++20 ×2
apache-spark ×1
atomic ×1
c++-modules ×1
consteval ×1
constructor ×1
cuda ×1
file ×1
filepath ×1
filesystems ×1
gzip ×1
nginx ×1
node.js ×1
overloading ×1
stdvector ×1
ubuntu ×1