为什么右值引用模板变量b<int>能够绑定到左值a?
#include <iostream>
int a = 3;
template<typename T>
T&& b = a;
int main() {
if(std::is_same_v<decltype(b<int>), int&&>) {
std::cout << "same\n";
} else {
std::cout << "different\n";
}
}
Run Code Online (Sandbox Code Playgroud)
Output: same
Run Code Online (Sandbox Code Playgroud) 从https://en.cppreference.com/w/cpp/memory/addressofstd::addressof上的可能实现来看,它指出“std::addressof 的正确实现需要编译器支持”。为什么会这样呢?
我在https://godbolt.org/z/vnzbs9aTG上尝试了该实现,它按预期工作。
#include <iostream>
#include <string>
#include <type_traits>
template<class T>
typename std::enable_if<std::is_object<T>::value, T*>::type addressof_impl(T& arg) noexcept
{
return reinterpret_cast<T*>(
&const_cast<char&>(
reinterpret_cast<const volatile char&>(arg)));
}
template<class T>
typename std::enable_if<!std::is_object<T>::value, T*>::type addressof_impl(T& arg) noexcept
{
return &arg;
}
struct Student {
std::string name{};
int age{};
};
int main() {
Student s;
std::cout << addressof_impl(s);
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud) 使用 type_identity 作为尾随返回类型的目的是什么?
https://en.cppreference.com/w/cpp/types/add_reference提供了以下可能的 add_lvalue_reference 和 add_rvalue_reference 实现
namespace detail {
template <class T>
struct type_identity { using type = T; }; // or use std::type_identity (since C++20)
template <class T> // Note that `cv void&` is a substitution failure
auto try_add_lvalue_reference(int) -> type_identity<T&>;
template <class T> // Handle T = cv void case
auto try_add_lvalue_reference(...) -> type_identity<T>;
template <class T>
auto try_add_rvalue_reference(int) -> type_identity<T&&>;
template <class T>
auto try_add_rvalue_reference(...) -> type_identity<T>;
} // namespace detail
template <class T> …Run Code Online (Sandbox Code Playgroud) 全局变量通常不被鼓励。C++ 标准库中具有外部链接的全局变量背后的原理是什么?
extern变量真的只是声明而不是定义吗?
std::call_once一个例子是声明具有mutex.h
外部链接的线程局部全局变量的 gcc 实现:
extern __thread void* __once_callable;
extern __thread void (*__once_call)();
Run Code Online (Sandbox Code Playgroud)
在
/// @cond undocumented
# ifdef _GLIBCXX_HAVE_TLS
// If TLS is available use thread-local state for the type-erased callable
// that is being run by std::call_once in the current thread.
extern __thread void* __once_callable;
extern __thread void (*__once_call)();
// RAII type to set up state for pthread_once call.
struct once_flag::_Prepare_execution
{
template<typename _Callable>
explicit
_Prepare_execution(_Callable& __c)
{
// Store address in thread-local pointer: …Run Code Online (Sandbox Code Playgroud) 注意:请不要仅仅因为根本原因是逗号运算符而关闭问题。这个问题的价值在于让社区了解以下方面的失败:
std::vector<std::pair<int, int>> vpairs_third{(0, 1), (0, -1)};
Run Code Online (Sandbox Code Playgroud)
是由于逗号运算符造成的。
我正在列表初始化一个包含整数对的向量。
我按照预期理解以下作品:
std::vector<std::pair<int, int>> vpairs_first{std::make_pair(0, 1), std::make_pair(0, -1)};
Run Code Online (Sandbox Code Playgroud)
std::vector<std::pair<int, int>> vpairs_second{{0, 1}, {0, -1}};
Run Code Online (Sandbox Code Playgroud)
但是,有人会解释为什么以下不起作用吗?
std::vector<std::pair<int, int>> vpairs_third{(0, 1), (0, -1)};
Run Code Online (Sandbox Code Playgroud)