#include <iostream>
using namespace std;
template<typename T>
void f(T&&) { cout << "f(T&&)" << endl; }
template<typename T>
void f(const T&&) { cout << "f(const T&&)" << endl; }
struct A {};
const A g1() { return {}; }
const int g2() { return {}; }
int main()
{
f(g1()); // outputs "f(const T&&)" as expected.
f(g2()); // outputs "f(T&&)" not as expected.
}
Run Code Online (Sandbox Code Playgroud)
问题描述嵌入在代码中.我的编译器是clang 5.0.
我只是好奇:
在这种情况下,为什么C++会以不同方式处理内置类型和自定义类型?
在 Linux 头文件中epoll.h,我找到了以下代码:
enum EPOLL_EVENTS
{
EPOLLIN = 0x001,
#define EPOLLIN EPOLLIN
...
}
Run Code Online (Sandbox Code Playgroud)
这是什么意思#define EPOLLIN EPOLLIN?
我的编译器是最新的VC++ 2013 RC.
void f()
{
int n1 = 0;
int n2 = reinterpret_cast<int>(n1); // error C2440
}
Run Code Online (Sandbox Code Playgroud)
错误C2440:'reinterpret_cast':无法从'int'转换为'int'
为什么在这种明显的情况下不能使用reinterpret_cast?
#include <set>
#include <string>
#include <cassert>
using namespace std::literals;
int main()
{
auto coll = std::set{ "hello"s };
auto s = "hello"s;
coll.insert(std::move(s));
assert("hello"s == s); // Always OK?
}
Run Code Online (Sandbox Code Playgroud)
C ++标准是否保证插入关联容器失败不会修改rvalue-reference参数?
c++ stl rvalue-reference language-lawyer pass-by-rvalue-reference
C++ 14支持通用lambda.但是,clang 3.4拒绝以下代码.
#include <utility>
void f(int);
void f(int&);
int main()
{
[](auto&& v) { f(std::forward<auto>(v)); }(8); // error
}
Run Code Online (Sandbox Code Playgroud)
如何auto&&在一般的lambda中完美前进?
#include <ranges>
#include <iostream>
#include <string_view>
using namespace std::literals;
int main()
{
auto fn_is_l = [](auto const c) { return c == 'l'; };
{
auto v = "hello"sv | std::views::filter(fn_is_l);
std::cout << *v.begin() << std::endl; // ok
}
{
auto const v = "hello"sv | std::views::filter(fn_is_l);
std::cout << *v.begin() << std::endl; // error
}
}
Run Code Online (Sandbox Code Playgroud)
请参阅:https : //godbolt.org/z/vovvT19a5
<source>:18:30: error: passing 'const std::ranges::filter_view<
std::basic_string_view<char>, main()::
<lambda(auto:15)> >' as 'this' argument discards
qualifiers [-fpermissive]
18 | std::cout << *v.begin() …Run Code Online (Sandbox Code Playgroud) #include <set>
#include <string>
#include <string_view>
using namespace std;
int main()
{
string_view key = "hello";
set<string> coll1;
coll1.find(key); // error
set<string, less<>> coll2;
coll2.find(key); // ok since C++14
}
Run Code Online (Sandbox Code Playgroud)
那么,它应该是一个规则:
总是喜欢 set<T, less<>> 对 set<T> ,因为C++ 14?
#include <type_traits>
int main()
{
auto f1 = [](auto&) mutable {};
static_assert(std::is_invocable_v<decltype(f1), int&>); // ok
auto const f2 = [](auto&) {};
static_assert(std::is_invocable_v<decltype(f2), int&>); // ok
auto const f3 = [](auto&) mutable {};
static_assert(std::is_invocable_v<decltype(f3), int&>); // failed
}
Run Code Online (Sandbox Code Playgroud)
查看演示
为什么 const mutable lambda 不能采用引用参数?
根据http://en.cppreference.com/w/cpp/io/streamsize
类型std :: streamsize是一个带符号的整数类型,用于表示在I/O操作中传输的字符数或I/O缓冲区的大小.
据我所知,流的大小永远不会是负数,因此,我的问题是:
为什么std::streamsize定义为签名而不是未签名?背后的理由是什么?
从C++ 11开始,我们可以写:
vector<int> v{1, 2, 3, 4};
for (auto x : v)
{
cout << x << endl;
}
Run Code Online (Sandbox Code Playgroud)
根据Essentials of Modern C++ Style,以下代码很快在C++中也是合法的:
vector<int> v{1, 2, 3, 4};
for (x : v)
{
cout << x << endl;
}
Run Code Online (Sandbox Code Playgroud)
这个功能是否可以在C++ 17或C++ 20中使用?
c++ ×10
standards ×4
c++11 ×3
c++20 ×2
lambda ×2
auto ×1
c ×1
c++14 ×1
constants ×1
convention ×1
idioms ×1
integer ×1
iostream ×1
linux ×1
overloading ×1
performance ×1
pointers ×1
signed ×1
std-ranges ×1
stl ×1
type-traits ×1
unsigned ×1