我很少使用模板.我不知道为什么我看到下面的代码生成错误的push方法Node<float>
构建错误是:没有匹配函数来调用push.
Node<int>* push方法很好.
Node<float>* head1 = NULL;
push(&head1, 1);
template <typename T>
struct Node {
T data;
Node* next;
};
template <typename T>
void push(Node<T>** head, T data) {
Node<T>* tmp = *head;
Node<T>* newNode = NULL; //createNode(data);
if (tmp == NULL) {
*head = newNode;
}
else {
while (tmp->next)
tmp=tmp->next;
tmp->next = newNode;
}
}
int main(int argc, const char * argv[]) {
Node<float>* head1 = NULL;
push(&head1, 1);
Node<int>* head = NULL;
push(&head, …Run Code Online (Sandbox Code Playgroud) #include<cstddef>
template<typename T, std::size_t N>
struct A {
T m_a[N];
A() : m_a{} {}
};
struct S {
explicit S(int i=4) {}
};
int main() {
A<S, 3> an;
}
Run Code Online (Sandbox Code Playgroud)
上面的代码与MSVC(2017)编译良好,但与clang 3.8.0(输出clang++ --version && clang++ -std=c++14 -Wall -pedantic main.cpp)失败:
clang version 3.8.0 (tags/RELEASE_380/final 263969)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/local/bin
main.cpp:6:15: error: chosen constructor is explicit in copy-initialization
A() : m_a{} {}
^
main.cpp:14:13: note: in instantiation of member function 'A<S, 3>::A' requested here
A<S, 3> an; …Run Code Online (Sandbox Code Playgroud) 在https://github.com/stlab/libraries/blob/main/stlab/concurrency/main_executor.hpp中,我读到
struct main_executor_type {
using result_type = void;
template <typename F>
void operator()(F f) const {
using f_t = decltype(f);
dispatch_async_f(dispatch_get_main_queue(), new f_t(std::move(f)), [](void* f_) {
auto f = static_cast<f_t*>(f_);
(*f)();
delete f;
});
}
};
Run Code Online (Sandbox Code Playgroud)
有什么意义decltype(f),为什么不简单地使用F呢?
我有以下功能(仅用于复制问题):
template <typename KeyT>
void func(const KeyT cptr) {
std::cout << typeid(KeyT).name() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
我想用字符串文字来调用它,如下所示:
func<char*>("literal");
Run Code Online (Sandbox Code Playgroud)
但是,我最终得到了警告:
warning: ISO C++11 does not allow conversion from string literal to 'char *' [-Wc++11-compat-deprecated-writable-strings]
我有一个特定需要使用char*作为我的主要类型,我期待TAD要考虑param type的const char*全部,因为我不是引用服用.
警告来自两者clang和g++编译器.
如何param type推断这里?
提前致谢.
是不是可以使用间接(取消引用)运算符取消引用指向存储在数组中的对象的指针,还是我做错了?
#include <iostream>
class A {
public:
virtual void test() {
std::cout << "A\n";
}
};
class B : public A {
public:
void test() {
std::cout << "B\n";
}
};
int main() {
A* v[2];
v[0] = new A();
v[1] = new B();
v[0]->test();
*(v[1]).test(); // Error! If the arrow operator is used instead
// though, the code compiles without a problem.
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误:
$ g++ -std=c++11 test.cpp && ./a.out
test.cpp: In function ‘int main()’:
test.cpp:26:13: error: …Run Code Online (Sandbox Code Playgroud) 这是最小的例子:
class A
{
A* const& this_ref;
public:
A() : this_ref(this) {}
};
Run Code Online (Sandbox Code Playgroud)
GCC 5.3.0发出警告:
警告:临时绑定到'A :: this_ref'只会持续存在,直到构造函数退出[-Wextra] A():this_ref(this){}
那是this暂时的吗?什么... MSVC 2015对此保持沉默,并且this_ref->member在我的情况下在构造函数之外引用类成员给出了预期的行为(但可能仅仅是UB的情况,不确定).
编辑:
请注意,这个问题扩展了一个链接尽可能重复,因为它不是关于创建此类引用的方法的一般性问题,而是关于警告GCC(以及除MSVC之外的其他可能的编译器)在创建时产生的.
这是std::thread构造函数声明的方式(使用Visual Studio 2015):
template<class _Fn,
class... _Args,
class = typename enable_if<
!is_same<typename decay<_Fn>::type, thread>::value>::type>
explicit thread(_Fn&& _Fx, _Args&&... _Ax)
Run Code Online (Sandbox Code Playgroud)
没有问题就_Fn和_Args,然而,第三class = ...混淆了我完全.它做什么,它是如何工作的以及它的用途是什么?
#include<iostream>
int num[3]={66,77,88};
int main()
{
int(*pi)[3]=#
std::cout<<*pi;
}
Run Code Online (Sandbox Code Playgroud)
结果是地址而不是数组。这背后的解释是什么?
为什么C++标准支持功能strftime()但不支持strptime()? strftime()可以将时间更改为字符串,但是没有可以将字符串更改回时间的函数.
在Posix strptime()上可以使用类似C的函数,使用它需要处理混合C和C++代码的潜在问题.
以下代码给出了悬空指针错误。
std::vector<std::pair<std::string, int>> c;
for (auto& b : c) {
const auto& [s, i] = b;
std::string_view v = s.substr(i);
std::cout << v;
}
Run Code Online (Sandbox Code Playgroud)
我认为它b保存了对 的引用std::pair<std::string, int>,所以s应该是对pair对象中字符串的引用。为什么这会产生悬空指针错误?我在这里缺少什么?godbolt 链接: https: //godbolt.org/z/4zMvbr