C ++ 20功能std::source_location
用于捕获有关调用函数的上下文的信息。当我尝试将其与可变参数模板函数一起使用时,遇到一个问题:我看不到放置source_location
参数的地方。
以下操作无效,因为可变参数必须在末尾:
// doesn't work
template <typename... Args>
void debug(Args&&... args,
const std::source_location& loc = std::source_location::current());
Run Code Online (Sandbox Code Playgroud)
以下内容也不起作用,因为调用者将被介于两者之间的参数所困扰:
// doesn't work either, because ...
template <typename... Args>
void debug(const std::source_location& loc = std::source_location::current(),
Args&&... args);
// the caller will get confused
debug(42); // error: cannot convert 42 to std::source_location
Run Code Online (Sandbox Code Playgroud)
可以在可变参数模板中无缝使用的评论中告诉我std::source_location
,但是我很难弄清楚该如何做。如何std::source_location
与可变参数模板函数一起使用?
c++ default-arguments variadic-templates c++20 std-source-location
复制其中一些成员未初始化的结构是否有效?
我怀疑这是未定义的行为,但如果是这样,则将任何未初始化的成员留在结构中(即使这些成员从未直接使用)非常危险。所以我想知道标准中是否有某些内容允许这样做。
例如,这有效吗?
struct Data {
int a, b;
};
int main() {
Data data;
data.a = 5;
Data data2 = data;
}
Run Code Online (Sandbox Code Playgroud) 不久之前,StackOverflow上有一个声誉很高的人在评论中写道,在调用(和类似的函数)之前必须抛出一个char
-argument .unsigned char
std::toupper
另一方面,Bjarne Stroustrup没有提到在C++ - Programming Language中这样做的必要性.他只是std::tolower
喜欢用
string name = "Niels Stroustrup";
void m3() {
string s = name.substr(6,10); // s = "Stroustr up"
name.replace(0,5,"nicholas"); // name becomes "nicholas Stroustrup"
name[0] = toupper(name[0]); // name becomes "Nicholas Stroustrup"
}
Run Code Online (Sandbox Code Playgroud)
(引用自该书,第4版.)
参考文献说输入需要表示为toupper
.对我来说,这听起来像每个unsigned char
以后都有,char
并且char
具有相同的大小.
那么这个演员是不必要的还是Stroustrup不小心?
编辑:libstdc ++手册提到输入字符必须来自基本源字符集,但不会强制转换.我想这是由@Keith Thompson的回复所涵盖的,他们都有积极的表现unsigned char
和signed char
?
考虑以下代码:
#include <cctype>
#include <functional>
#include <iostream>
int main()
{
std::invoke(std::boolalpha, std::cout); // #1
using ctype_func = int(*)(int);
char c = std::invoke(static_cast<ctype_func>(std::tolower), 'A'); // #2
std::cout << c << "\n";
}
Run Code Online (Sandbox Code Playgroud)
在此,对的两个调用std::invoke
已标记为将来参考。预期的输出是:
a
Run Code Online (Sandbox Code Playgroud)
在C ++ 20中可以保证预期的输出吗?
(注意:有两个函数tolower
,一个称为in <cctype>
,另一个称为in <locale>
。引入了显式强制转换以选择所需的重载。)
c++ c++-standard-library language-lawyer unspecified-behavior c++20
I have 2 columns of tab delimited integers, the first of which is a random integer, the second an integer identifying the group, which can be generated by this program. (generate_groups.cc
)
#include <cstdlib>
#include <iostream>
#include <ctime>
int main(int argc, char* argv[]) {
int num_values = atoi(argv[1]);
int num_groups = atoi(argv[2]);
int group_size = num_values / num_groups;
int group = -1;
std::srand(42);
for (int i = 0; i < num_values; ++i) {
if (i % group_size == 0) …
Run Code Online (Sandbox Code Playgroud) 我对编写一个memcpy()
教育练习感兴趣.我不会写一篇关于我做了什么和没想过的论文,但这里
有一些人的实现:
__forceinline // Since Size is usually known,
// most useless code will be optimized out
// if the function is inlined.
void* myMemcpy(char* Dst, const char* Src, size_t Size)
{
void* start = Dst;
for ( ; Size >= sizeof(__m256i); Size -= sizeof(__m256i) )
{
__m256i ymm = _mm256_loadu_si256(((const __m256i* &)Src)++);
_mm256_storeu_si256(((__m256i* &)Dst)++, ymm);
}
#define CPY_1B *((uint8_t * &)Dst)++ = *((const uint8_t * &)Src)++
#define CPY_2B *((uint16_t* &)Dst)++ = *((const uint16_t* &)Src)++
#define CPY_4B …
Run Code Online (Sandbox Code Playgroud) 如果使用VC ++ 2017编译,下面的代码将显示垃圾(或零),如果使用GCC或Clang(https://rextester.com/JEV81255)进行编译,则以下代码将显示“ 1122” 。是VC ++的bug还是我在这里遗漏了一些东西?
#include <iostream>
struct Item {
int id;
int type;
};
int main()
{
auto items = new Item[2]
{
{ 1, 1 },
{ 2, 2 }
};
std::cout << items[0].id << items[0].type;
std::cout << items[1].id << items[1].type;
}
Run Code Online (Sandbox Code Playgroud)
同时,如果元素属于原始类型(例如int
),则也可以使用。
c++ new-operator visual-c++ compiler-bug aggregate-initialization
在计算机科学中,有两个难题:缓存失效,命名和一个错误。
这是关于第二个问题的:命名事物。
我在寻找这种技术或类型是否已经在其他地方使用并且有名称。 dichotomy
是一个不错的名字,但那bools_at_compile_time
是一个可怕的名字。
using dichotomy_t = std::variant<std::false_type, std::true_type>;
// (or a struct that inherits from that, and overloads operator bool())
constexpr dichotomy_t dichotomy( bool b ) {
if (b) return std::true_type{};
return std::false_type{};
}
template<class F, class...Bools>
constexpr auto bools_at_compile_time( F&& f, Bools...bools ) {
static_assert( (std::is_same<Bools, bool>{} && ...) );
return std::visit( std::forward<F>(f), dichotomy(bools)... );
}
Run Code Online (Sandbox Code Playgroud)
dichotomy_t
是true和false之间的变体。其运行时表示为0
或1
。
这可以让您做的是:
auto foo( bool x, bool y ) { // <-- x and …
Run Code Online (Sandbox Code Playgroud) 可能重复:
您可以在初始化后调整C++数组的大小吗?
我需要在C++中使用以下C#代码
Array.Resize(ref A, A.Length - 1);
Run Code Online (Sandbox Code Playgroud)
如何在C++中实现这一目标?
我很想知道它是如何nullptr
工作的。标准 N4659 和 N4849 说:
std::nullptr_t
;sizeof(std::nullptr_t) == sizeof(void*)
;bool
为false
;(void*)0
,但不能向后转换;所以它基本上是一个与 具有相同含义的常量(void*)0
,但它具有不同的类型。我std::nullptr_t
在我的设备上找到了实现,如下所示。
#ifdef _LIBCPP_HAS_NO_NULLPTR
_LIBCPP_BEGIN_NAMESPACE_STD
struct _LIBCPP_TEMPLATE_VIS nullptr_t
{
void* __lx;
struct __nat {int __for_bool_;};
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR nullptr_t() : __lx(0) {}
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR nullptr_t(int __nat::*) : __lx(0) {}
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR operator int __nat::*() const {return 0;}
template <class _Tp>
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
operator _Tp* () const {return 0;}
template <class _Tp, class …
Run Code Online (Sandbox Code Playgroud) c++ ×9
c++17 ×2
c++20 ×2
avx ×1
boolean ×1
c ×1
compiler-bug ×1
new-operator ×1
null-pointer ×1
nullptr ×1
optimization ×1
performance ×1
simd ×1
tolower ×1
toupper ×1
variant ×1
visual-c++ ×1
x86 ×1