小编son*_*yao的帖子

在 C++ 中初始化变量的 = 和 {} 语法之间的区别

我已经阅读了很多 C++ 代码,并且遇到了两种初始化变量的方法。

方法一:

int score = 0;
Run Code Online (Sandbox Code Playgroud)

方法二:

int score {};
Run Code Online (Sandbox Code Playgroud)

我知道这int score {};会将分数初始化为 0,所以也会int score = 0;

这两者有什么区别?我已经阅读了初始化:括号与等号,但这并没有回答我的问题。我想知道等号大括号之间有什么区别,而不是括号。在什么情况下应该使用哪一个?

c++ initialization c++11

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

使用基成员函数引入派生类的重载解析

根据标准中的一些引用:
[over.match.funcs]/4

[...] 对于通过 using 声明引入派生类的非转换函数,为了定义隐式对象参数的类型,该函数被认为是派生类的成员。

并考虑以下代码:

#include <iostream>
struct Base {
    void show(double) {
        std::cout << "0" << std::endl;
    }
};
struct Test :Base {
    using Base::show;  //#1
    void show(double) { //#2
        std::cout << "1" << std::endl;
    }
};
int main() {
    Test t;
    t.show(1.2);
}
Run Code Online (Sandbox Code Playgroud)

根据我引用的标准,这意味着将类型的隐式对象参数Test作为 #1.
对于#1,声明将是show(Test&,double)
对于#2,声明将是show(Test&,double)
所以为了重载解析的目的,每个隐式转换序列 的 与 的#1没有区别#2, 的调用t.show(1.2)会模棱两可,但是#2调用,为什么?如果我遗漏了标准中的某些内容,请纠正我。

c++ using-declaration language-lawyer

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

未见类外的函数重载

考虑以下代码片段:

enum class Bar {
    A
};

void foo(Bar) {}

struct Baz {
    void foo() {
        foo(Bar::A);
    }
};
Run Code Online (Sandbox Code Playgroud)

它无法编译,来自 gcc 9.2 的消息是:

:12:19: error: no matching function for call to 'Baz::foo(Bar)'
 12 |         foo(Bar::A);
    |              
Run Code Online (Sandbox Code Playgroud)

我不怀疑这是一个错误,因为 clang 10 也失败了。关于这种情况,我有两个问题:

  1. 标准在哪里定义了这种重载的行为?

  2. 以这种方式指定编译器行为的可能原因是什么?

活生生的例子

c++ overloading language-lawyer name-lookup

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

C++ 模板错误:“=”标记之前缺少模板参数

我正在尝试创建一个设置 LinkedList 根节点的函数。但是,当我运行以下代码时:

\n\n
#include <iostream>\nusing namespace std;\n\ntemplate <typename K>\nstruct Node {\n  Node<K>* next;\n  const K value;\n};\n\ntemplate <typename K>\nNode<K>* root = NULL;\n\ntemplate <typename K>\nvoid SetRoot(const K &key) {\n    Node<K> new_node = Node<K> {NULL, key};\n    root = &new_node;\n}\n\nint main(int argc, char *argv[])\n{\n     Node<int> n1 = Node<int> {NULL, 48};\n     SetRoot(n1);\n\n    return 0;\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

我在该行收到此错误root = &new_node;

\n\n
\n

错误: \xe2\x80\x98=\xe2\x80\x99 令牌根 = &new_node; 之前缺少模板参数;

\n
\n\n

但是,new_node确实具有 struct 的所有预期参数Node

\n

c++ templates struct

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

c ++如何将右值引用传递给另一个函数

我正在使用嵌入式模板化库 etl::queue

https://www.etlcpp.com/queue.html

etl::queueIS quitvalent到std::queue

为了避免复制,我想将元素实际移动到队列中。

现在我的设置看起来像这样

  bool CETLConcurrentQueue<ElementType_T, u32QueueSize>::Add(ElementType_T &&element, const uint32_t u32Timeout)
{
   //lock mutex...
   queue.push(element);
   //do further stuff
}
Run Code Online (Sandbox Code Playgroud)

现在我不使用了,queue.push(std::move(element));因为 element 已经是一个右值引用了

但是,queue.push(element);调用元素复制构造函数(已删除)如何改为调用元素移动构造函数?

c++ move-constructor move-semantics c++11

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

std::is_class 在引用类上为 false

为什么std::is_classfalse我测试它的参考?

int main() {
    struct foo_struct {
        int i1;
        int i2;
    };
    std::cout << std::boolalpha << std::is_class<foo_struct>::value << std::endl; // true
    std::cout << std::boolalpha << std::is_class<foo_struct&>::value << std::endl; // falae
}
Run Code Online (Sandbox Code Playgroud)

c++ reference class type-traits c++17

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

C++ 用于使用逗号运算符的多个控制语句

如果在“for 循环”中使用逗号运算符来编写多个控制语句,它如何?我试过

#include <iostream>

using namespace std;

int main() {
        for (int x = 0, y = 0; x < 3, y < 4; ++x, ++y) {
                cout << x << " " << y << endl;
        }
        return 0;
}

Run Code Online (Sandbox Code Playgroud)

似乎只计算最后一个表达式。泰

c++ for-loop comma-operator

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

在 C++ 中,将左值完美转发到函数模板的正确方法是什么?

接受完美转发的参数包的正确方法是什么,以便它可以采用任何类型并简单地转发它们?以下代码适用于常规类型,但不适用于指针类型:

template<typename ...A>
void b(A &&... args)
{}

template<typename ...A>
void a(A &&... args)
{
    b(std::forward<A>(args)...);
}


int main() {

    // ok
    a<int>(5);

    // error: cannot bind rvalue reference of type ‘int*&&’ to lvalue of type ‘int*’
    int *foo = nullptr;
    a<int*>(foo);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

[编辑] 感谢您的快速而精彩的回复!我过于简化了 - 这是我试图解决的问题的一个更接近的例子:

#include <iostream>
using namespace std;

template<typename F>
struct fun;

template<typename F, typename ...A>
struct fun<F(A...)>
{
    void b(A &&... args)
    {}
    
    void a(A &&... args)
    {
        b(std::forward<A>(args)...);
    }
};

int main() …
Run Code Online (Sandbox Code Playgroud)

c++ templates perfect-forwarding c++11

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

当按值发送参数时,为什么默认复制构造函数不调用?

该程序初始化一个类的对象并将其作为参数传递给另一个成员函数。当我发表声明时

Address a2=a1;
Run Code Online (Sandbox Code Playgroud)

它显示没有错误。但是当我将参数作为对象时

Employee(int id, string name, Address address);
Run Code Online (Sandbox Code Playgroud)

并调用它使用

Employee e1 = Employee(101,"Nakul",a2);
Run Code Online (Sandbox Code Playgroud)

它显示以下错误。

#include <iostream>  
using namespace std;  
class Address {  
    public:  
     string addressLine, city, state;    
     Address(string addressLine, string city, string state)    
     {    
        this->addressLine = addressLine;    
        this->city = city;    
        this->state = state;    
     }
    
};  
class Employee    
{    
     private:  
     Address address;  //Employee HAS-A Address   
     public:  
     int id;    
     string name;    
     Employee(int id, string name, Address address)    //Here it is showing an error
     {    
           this->id = id;    
           this->name = name; …
Run Code Online (Sandbox Code Playgroud)

c++ oop constructor

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

为什么将 int 分配给 std::map&lt;string,string&gt; 不会产生编译器错误

尝试将 int 分配给字符串\n std::string s = 5;\n会产生以下编译器错误:

\n

error: conversion from \xe2\x80\x98int\xe2\x80\x99 to non-scalar type \xe2\x80\x98std::string\xe2\x80\x99 {aka \xe2\x80\x98std::__cxx11::basic_string<char>\xe2\x80\x99} requested

\n

但是,将 int 分配给映射中的字符串值则不然。例如,下面的代码可以编译,更糟糕的是使赋值将 int 转换为 char

\n
map <string, string>m;\nm["test"] = 5;\n
Run Code Online (Sandbox Code Playgroud)\n

这不应该是一个错误吗?

\n

c++ initialization stdstring variable-assignment

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