小编woh*_*tad的帖子

从立即函数返回 std::vector

从 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++ stdvector language-lawyer c++20 consteval

10
推荐指数
1
解决办法
600
查看次数

无法在 Visual Studio 2022 中使用 C++20 标准模块

我正在尝试使用 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

c++ visual-studio c++20 c++-modules

8
推荐指数
0
解决办法
3976
查看次数

Spark SQL中的递归cte

; 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 中实现同样的目标吗

apache-spark apache-spark-sql spark-notebook

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

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

C++17 根据文件路径自动创建目录

#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。

c++ filesystems ubuntu file filepath

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

std::unordered_map insert 仅使迭代器无效,但不会使指向元素节点的引用和指针无效

有人可以解释为什么插入到std::unordered_map容器中只会使迭代器无效,而不会使引用和指针无效。另外,我无法理解 https://en.cppreference.com/w/cpp/container/unordered_map/insert 中的以下声明的含义

如果插入成功,则在元素保存在节点句柄中时获得的指向该元素的指针和引用将失效,而在提取该元素之前获得的指向该元素的指针和引用将变得有效。

c++ unordered-map

6
推荐指数
2
解决办法
614
查看次数

类静态成员数组的范围 static_assert

我想使用 来强制执行关于类静态成员变量数组长度的编译时约束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)

c++

6
推荐指数
0
解决办法
67
查看次数

将 std::unique_ptr 保存为不完整类型的类的构造函数定义之间的差异

下面的代码编译正常(参见下面的 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)

c++ constructor language-lawyer incomplete-type

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

cuda::atomic 到底是做什么的?

我刚刚发现 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)

cuda atomic

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

在 C++ 中使用 size_t sz 的附加参数重载运算符 delete

下面的代码允许您查看被删除的变量的大小:

#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

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