小编Gui*_*cot的帖子

catch 内的 co_await 不再使用 GCC12 进行编译

我有一个看起来像这样的代码:

auto func() -> asio::awaitable<void> {
    try {
        co_await async_operation();
    } catch(boost::system::system_error const& e) {
        co_return co_await another_async_operation();
    }
}
Run Code Online (Sandbox Code Playgroud)

此代码在 GCC 11 中完美运行,但在 GCC 12 中无法编译:

file.cpp:3:19: error: await expressions are not permitted in handlers
  3 |         co_return co_await another_async_operation();
    |                   ^~~~~~~~
Run Code Online (Sandbox Code Playgroud)

这是为什么?我该如何解决它?

c++ gcc c++20 c++-coroutine gcc12

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

模板继承:没有依赖于模板参数的参数

编译以下代码时遇到此错误.在做了一些研究并在不同情况下阅读类似的错误之后,我想出了我需要的解决方案.但我没有完全理解错误和修复的不正当理由.

template <typename T>
class TestA {
    int a;
    T temp;

protected:
    int b; 

public: 
    int c;

    TestA(T te): a{10}, b{20}, c{30}, temp{te} {}

    int geta(){ return a; }
    int getb(){ return b; }
    int getc(){ return c; }
};

template <typename T>
class TestB {
    int atb;
    T tempb;

protected:
    int btb; 

public: 
    int ctb;

    TestB(T te) atb{10}, btb{20}, ctb{30}, tempb{te} {}
};

template <typename T>
class TestInh : public TestA<T>, public TestB<T> {
    int aa;
    T temptemp;

protected: …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance templates

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

使用constexpr和函数

我正在尝试使用constexpr和static_assert.我实际上需要检查constexpr字符串的长度,该字符串由专用函数计算.这是我正在尝试运行的:

#include <iostream>
using namespace std;

class Test
{
    private :
        static constexpr char str[] = "abc";

        static int constexpr constStrLength(const char* str)
        {
            return *str ? 1+constStrLength(str+1) : 0;
        }
        static constexpr int length = constStrLength(str);
        static_assert(length ==3, "error");

    public :
        static void f()
        {
            cout << len << endl;
        }
};

int main()
{
    Test::f();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这是我得到的错误:

错误:'static constexpr int Test :: constStrLength(const char*)'在常量表达式中调用static constexpr int len = constStrLength("length");

什么是实现它的正确方法?

感谢帮助 !

c++ constexpr

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

为什么模板函数不会出现在LLVM-IR中?

为什么模板函数在LLVM-IR中不显示,如果没有调用函数,从c ++代码发出LLVM IR,不像其他类型的函数(int,float ...)将出现在llvm ir示例中:以下函数func1不会在llvm ir中显示

template <class tmp>
tmp func1 () {
    // ...
}
Run Code Online (Sandbox Code Playgroud)

但是这个功能func2总是在llvm ir中显示

int func2 () {
    // ...
}
Run Code Online (Sandbox Code Playgroud)

c++ llvm llvm-clang llvm-ir

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

多个指针指向相同的数据

int a = 10; 
int* ptrA = &a;
int* ptrB = &a;

delete ptrA;

return 0;
Run Code Online (Sandbox Code Playgroud)

这不应该导致泄漏,导致mem/data被释放ptrA,但是我们有2个指向有效地址的指针,其数据刚被删除?它是如何工作的?

c++ memory

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

如果类数组中的向量是动态推入后退的,内存将如何工作?

假设我们有以下课程:

class Test {
public:
   Test() {}
   std::vector<int>& getIntList() {
      return intList;
   }
private:
   std::vector<int> intList;
};
Run Code Online (Sandbox Code Playgroud)

同样,我们在main函数中有以下代码来声明类数组:

int main(void) {
   Test* test[20];

   for (int i = 0; i < 20; ++i) {
      test[i] = new Test();
   }
}
Run Code Online (Sandbox Code Playgroud)

在这些情况下,将实例化测试对象。

现在,如果我在每个类中的向量中随机添加多个项目,

在调整其内存大小的同时,可能会碰撞每个类中每个向量的内存地址范围。

在这种情况下,是否将整个“测试”对象复制到其他存储区中,并调整矢量的大小?或者,向量STL是仅在类引用向量时才复制到其他存储区并调整大小吗?

完全地,这样编码不是一个好主意吗?

c++ class vector memory-reallocation

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

如何将大括号初始化器用于动态大小数组和new?

我有一个float*“数组”(不确定它是否正确),我正在传递给另一个函数。通常,我会这样做,float *array = new float[6];但是现在我要输入一些临时代码,因此我想立即设置所有值。有没有办法我可以做这样的事情:

float *array = new float[] { 1, 2, 3, 4, 5, 6 };
Run Code Online (Sandbox Code Playgroud)

并让它推断出大小,因此我不必这样做:

float *array = new float[6];
array[0] = 1;
array[1] = 2;
...
Run Code Online (Sandbox Code Playgroud)

c++ arrays new-operator

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

如何使用模板为“ std :: array”声明“ using”

例如,我有类似的代码:

template<typename A, typename B>
using Map = std::map<A, B>;

template<typename A, typename B>
using UnorderedMap = std::unordered_map<A, B>;
Run Code Online (Sandbox Code Playgroud)

我想对做同样的事情std::array,即:

template<typename A, typename B>
using Array = std::array<A, B>;
Run Code Online (Sandbox Code Playgroud)

但是,如果这样做,我将得到一个编译器错误:

错误C2993:'B':非类型模板参数'_Size'的非法类型

错误C2955:“ std :: array”:使用类模板需要模板

参数列表array(21):message:参见'std :: array'的声明

有什么方法可以声明Array在后台使用的std::array吗?

最后,我想使用Array<int, 7> items代替的数组std::array<int, 7> items。

c++ arrays templates std stdarray

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

为什么要更改这两个代码的输出中的“&”字符?

第一个代码:

int a = 1;

void func(int* ptr) {
    ptr = &a;
}

int main() {
    int nvar = 2;
    int* pvar = &nvar;
    func(pvar);
    std::cout << *pvar;
}
//Output: 2
Run Code Online (Sandbox Code Playgroud)

第二个代码:

int a = 1;

void func(int*& ptr) {
    ptr = &a;
}

int main() {
    int nvar = 2;
    int* pvar = &nvar;
    func(pvar);
    std::cout << *pvar;
}
//Output: 1
Run Code Online (Sandbox Code Playgroud)

唯一的区别是'func'函数中的'&'字符。但是有人可以解释一下我,在这种情况下会做什么?

c++ pointers reference dereference

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

我如何为可以是字符或字符串的模板类实现方法?

我有这门课

template <typename T>
class B{
    T x;
public:
    B(char c){
        if(typeid(x)==typeid(char)) x=c;
        else{
            string h;
            for(int i=0;i<10;i++) h.push_back(c);
            x=h;
        }
    }
};
Run Code Online (Sandbox Code Playgroud)

它是一个示例类,如果 x 的类型是 char 我想要 x=c 而如果 x 是一个字符串我想要 x 是 [c]^10

然后我尝试创建两个对象:

int main(){
    B<string> a('f');
    B<char> b('g');     
}
Run Code Online (Sandbox Code Playgroud)

当 i 对象 b 被实例化时,编译器在第 10 行产生一个错误:

[Error] cannot convert 'std::string {aka std::basic_string<char>}' to 'char' in assignment
Run Code Online (Sandbox Code Playgroud)

我知道错误来自于您无法将字符串分配给字符的事实,但无论如何我都需要完成任务,我该怎么做?

c++ templates class

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