我发现这里的代码看起来像这样:
auto f(T& t, size_t n) -> decltype(t.reserve(n), void()) { .. }
Run Code Online (Sandbox Code Playgroud)
在我阅读的所有文档中,我被告知decltype
签名为:
decltype( entity )
要么
decltype( expression )
任何地方都没有第二个论点.至少这是cppreference所指出的.这是第二个参数decltype
吗?如果是这样,它会做什么?
标准C++库中的所有名称都是小写的,除了std::ios_base::Init
.为什么是这样?
我很少看到,decltype(auto)
但是当我这样做时,它会让我感到困惑,因为它似乎与auto
从函数返回时一样.
auto g() { return expr; }
decltype(auto) g() { return expr; }
Run Code Online (Sandbox Code Playgroud)
这两种语法有什么区别?
我看到decltype(x)
在宏中使用的x
是一个变量名,因为在宏内部不知道对象的类型.
例如:
decltype(x) y = expr;
Run Code Online (Sandbox Code Playgroud)
我可以轻松使用auto
而不是decltype
.那么decltype
变量类型声明需要哪些情况而不是auto
?
为什么不vector::push_back
采用转发引用而不是两次重载?我已经读过你想要在左值和右值上重载的唯一原因是你的函数是否为它们做了不同的事情,那么vector::push_back
除了移动/复制之外,两个重载的不同之处是什么?
我正在阅读std::ignore
cppreference 的文档.我发现很难掌握这个对象的真正目的,并且示例代码并没有做到这一点.例如,在下面的代码中,如何以及为什么inserted
设置为true?这对我来说没什么意义.
#include <iostream>
#include <string>
#include <set>
#include <tuple>
int main()
{
std::set<std::string> set_of_str;
bool inserted;
std::tie(std::ignore, inserted) = set_of_str.insert("Test");
if (inserted) {
std::cout << "Value was inserted sucessfully\n";
}
}
Run Code Online (Sandbox Code Playgroud)
如果有人可以向我解释代码,我们将不胜感激.谢谢.
我能否在C++ 1y(C++ 14)中为main函数执行以下操作:
auto main()
{
// ...
}
Run Code Online (Sandbox Code Playgroud)
int
即使我们不需要使用显式,返回类型也会自动生成return 0;
吗?
我试图A<F>::f()
从类中调用类函数S
,但是当我实例化一个S
object(S<int>s
)并调用它的f
成员函数(s.f()
)时,我得到以下错误:
source.cpp:实例化'
int S<F>::f() [with F = int]
':
source.cpp:30:21:从这里需要
source.cpp:22:25:错误:'A<int>
'不是' '的可访问基础'S<int>
'
需要注意的是这个工程的时候我更换return A<F>::f();
类的声明中S
使用return C<A, F>::f();
.但我想知道为什么我不能这样做...
#include <iostream>
template <typename T> class A {
public:
int f();
};
template <typename T> int A<T>::f() {
return sizeof(T);
}
template <template <typename> class E, typename D> class C : E<D> {
public:
int f() {
return E<D>::f();
}
};
template …
Run Code Online (Sandbox Code Playgroud) 我使用以下代码获得了最新版本的clang和gcc的错误:
int main() {
auto lambda = [] (auto = [] {}) {};
lambda();
}
Run Code Online (Sandbox Code Playgroud)
Clang给出错误:
prog.cc: In function 'int main()':
prog.cc:3:12: error: no match for call to '(main()::<lambda(auto:1)>) ()'
lambda();
^
prog.cc:2:35: note: candidate: template<class auto:1> main()::<lambda(auto:1)>
auto lambda = [] (auto = [] {}) {};
^
prog.cc:2:35: note: template argument deduction/substitution failed:
prog.cc:3:12: note: couldn't deduce template parameter 'auto:1'
lambda();
^
Run Code Online (Sandbox Code Playgroud)
为什么这会失败?
在维基百科上,我发现了这个:
A a( A() );
Run Code Online (Sandbox Code Playgroud)
[这]可以消除歧义
class [
A
] 的变量定义,取一个类[A
]或的匿名实例函数的函数声明,它返回一个类型为[
A
] 的对象,并获取一个(未命名的)参数,该参数是一个返回类型[A
](并且不进行输入)的函数.大多数程序员都期望第一个,但C++标准要求它被解释为第二个.
但为什么?如果大多数C++社区都期望以前的行为,为什么不把它作为标准呢?此外,如果不考虑解析歧义,则上述语法是一致的.
有人可以赐教吗?为什么标准要求这个?