小编son*_*yao的帖子

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
查看次数

将 std::make_pair 与 std::string 一起使用(非右值引用问题)

std::pair<Url, std::string> UrlParser::parse()
{
    return std::make_pair({ extract_scheme(), extract_hostname(), extract_port(),
                 extract_path(), extract_filename() }, host_ip_);
}
Run Code Online (Sandbox Code Playgroud)

host_ip_变量被定义为

std::string host_ip_;
Run Code Online (Sandbox Code Playgroud)

我得到

UrlParser.cpp:91:64: error: no matching function for call to 'make_pair(<brace-enclosed initializer list>, std::string&)'
   91 |                  extract_path(), extract_filename() }, host_ip_);
Run Code Online (Sandbox Code Playgroud)

问题出在host_ip_变量上。如果是std::string,那么返回它有什么问题?

在 `std::make_pair` 中发现了c++11 右值引用,它解释了我们不能std::make_pair用非右值引用调用,所以我试过了

std::make_pair({ extract_scheme(), extract_hostname(), extract_port(),
                     extract_path(), extract_filename() }, std::move(host_ip_));
Run Code Online (Sandbox Code Playgroud)

但我明白了

error: no matching function for call to 'make_pair(<brace-enclosed initializer list>, std::remove_reference<std::__cxx11::basic_string<char>&>::type)'
   91 |                  extract_path(), extract_filename() }, std::move(host_ip_));
Run Code Online (Sandbox Code Playgroud)

顺便说一下,为什么在提供的链接中, …

c++ templates template-argument-deduction

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

为什么要调用构造函数?

为什么s1()在这里调用构造函数?

#include<iostream>

struct s1 {
    s1(int tmp) {

    }

    s1() {
        std::printf("s1 invoked");
    }
};

struct s2 {
    s1 s;
    s2(s1& s) {
        this->s = s;
    }
};
int main() {

    s1 o1(5);
    s2 o2(o1);
    return 0;
}

Run Code Online (Sandbox Code Playgroud)

结果: s1 invoked

为什么在s2(s1& s)调用s1()构造函数中按引用传递?

c++ constructor initialization

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

接收迭代器并返回向量的函数

我的任务是创建一个与以下相同的函数

template <typename It>
auto MakeSet(It range_begin, It range_end) {
     return set(range_begin, range_end);
}
Run Code Online (Sandbox Code Playgroud)

但它必须是一个模板,它接收迭代器(begin() 和 end())并返回一个包含范围内元素的向量。

我试过

template<typename Type, typename It>
auto MakeVector(It range_begin, It range_end) {
    return vector<Type> v(range_begin, range_end);
}
Run Code Online (Sandbox Code Playgroud)

但是我收到以下错误

无法识别的模板声明/定义

c++ iterator stdvector

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

clang、gcc 和忽略限定符:谁是对的?

我在编译 BoringSSL 时发现 gcc 和 clang 之间的行为差​​异,并且能够将其缩减为以下测试用例来说明:

\n
typedef char *OPENSSL_STRING;\n#if USE_TYPEDEF\n#define constptr const OPENSSL_STRING\n#else\n#define constptr const char *\n#endif\n\nint\nfoo (const void **ap)\n{\n    constptr a = (constptr) *ap;\n    return a != 0;\n}\n
Run Code Online (Sandbox Code Playgroud)\n

我测试了四种场景,如下:

\n
sh$ g++ -c t2.cc -Wignored-qualifiers -DUSE_TYPEDEF\nt2.cc: In function \xe2\x80\x98int foo(const void**)\xe2\x80\x99:\nt2.cc:11:30: warning: type qualifiers ignored on cast result type [-Wignored-qualifiers]\n   11 |     constptr a = (constptr) *ap;\n      |                              ^~\nsh$ g++ -c t2.cc -Wignored-qualifiers \nsh$ clang++ -c t2.cc -Wignored-qualifiers -DUSE_TYPEDEF\nsh$ clang++ -c t2.cc -Wignored-qualifiers \nsh$ \n
Run Code Online (Sandbox Code Playgroud)\n …

c++ gcc qualifiers clang boringssl

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