我正在尝试创建一个设置 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}\nRun Code Online (Sandbox Code Playgroud)\n\n我在该行收到此错误root = &new_node;:
\n\n\n错误: \xe2\x80\x98=\xe2\x80\x99 令牌根 = &new_node; 之前缺少模板参数;
\n
但是,new_node确实具有 struct 的所有预期参数Node。
我正在使用嵌入式模板化库 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);调用元素复制构造函数(已删除)如何改为调用元素移动构造函数?
为什么std::is_class是false我测试它的参考?
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) 如果在“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)
似乎只计算最后一个表达式。泰
接受完美转发的参数包的正确方法是什么,以便它可以采用任何类型并简单地转发它们?以下代码适用于常规类型,但不适用于指针类型:
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) 该程序初始化一个类的对象并将其作为参数传递给另一个成员函数。当我发表声明时
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) 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)
顺便说一下,为什么在提供的链接中, …
为什么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()构造函数中按引用传递?
我的任务是创建一个与以下相同的函数
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)
但是我收到以下错误
无法识别的模板声明/定义
我在编译 BoringSSL 时发现 gcc 和 clang 之间的行为差异,并且能够将其缩减为以下测试用例来说明:
\ntypedef 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}\nRun Code Online (Sandbox Code Playgroud)\n我测试了四种场景,如下:
\nsh$ 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$ \nRun Code Online (Sandbox Code Playgroud)\n … c++ ×10
templates ×3
c++11 ×2
constructor ×2
boringssl ×1
c++17 ×1
clang ×1
class ×1
for-loop ×1
gcc ×1
iterator ×1
oop ×1
qualifiers ×1
reference ×1
stdvector ×1
struct ×1
template-argument-deduction ×1
type-traits ×1