language-lawyer

Can a constexpr c-string used as template parameter be instantiated in a template function?

南楼画角 提交于 2021-01-27 12:49:16
问题 I have been prototyping a library in which I would like to use a c-string value defined locally in a function as a non-type template parameter. In essence, the bare minimum implementation is the following (see on godbolt): #include <cstdio> #include <type_traits> template <char const* C> class compile_string {}; template <typename T> auto foo() { static constexpr char const CSTR[] = "Hello, World!"; return compile_string<CSTR>{}; } int main() { auto shorty = foo<short>(); auto inty = foo<int>

Implicitly defined constructor deleted due to variant member, N3690/N4140 vs N4659/N4727

纵饮孤独 提交于 2021-01-27 12:12:32
问题 My story starts off the same as this person's here: Unions in C++11: default constructor seems to be deleted The resolution here (now about three years old) is a bit unsatisfactory, because the "Digging into the standard" that the author did ended up with concluding that the behavior was as described in the standard, but unfortunately the quote is from a Note , and those are supposed to be non-normative (I've been told). Anyways, there's a link to an old bug report on gcc that is claimed to

Does a class template's requires clause have to be repeated outside member definitions?

只谈情不闲聊 提交于 2021-01-27 06:59:29
问题 When the member of a class template that uses the requires clause is defined outside the class, gcc does not complain if requires is not specified, whereas clang does. Consider the code snippet below: #include <concepts> template<typename Container> requires std::integral<typename Container::value_type> class Foo { public: void func(); }; template<typename Container> void Foo<Container>::func() {} The compilation using gcc does not complain. While clang reports the following error: ❯ clang++

static global variables initialization order

喜欢而已 提交于 2021-01-27 06:59:09
问题 In many of the answers that I found here were said the following words: Global variables in a single translation unit (source file) are initialized in the order in which they are defined. or Within the same compilation unit the order is well defined: The same order as definition. etc. But where can I see these words in the standard of C++? I would like to get a one or few concrete paragraph's where such behavior is described. I can not find it myself, and I do not know who to ask. 回答1: 6.6.3

Does a class template's requires clause have to be repeated outside member definitions?

你离开我真会死。 提交于 2021-01-27 06:57:26
问题 When the member of a class template that uses the requires clause is defined outside the class, gcc does not complain if requires is not specified, whereas clang does. Consider the code snippet below: #include <concepts> template<typename Container> requires std::integral<typename Container::value_type> class Foo { public: void func(); }; template<typename Container> void Foo<Container>::func() {} The compilation using gcc does not complain. While clang reports the following error: ❯ clang++

Template method accesses forward declared class fails to compile only without this pointer

99封情书 提交于 2021-01-27 06:56:38
问题 When I compile the following code with the latest Visual Studio, it success to compile. class C; class T { public: template<typename A> void f(); private: C* c; }; int main() { T t; t.f<int>(); } template<typename A> void T::f() { this->c->g(); } class C { public: void g() {} }; But when I remove this-> from this->c->g() , compilation fails with C2027: use of undefined type 'C' . When I make the method f non-template, it fails to compile no matter this-> presents or not, so I think it's

Template method accesses forward declared class fails to compile only without this pointer

徘徊边缘 提交于 2021-01-27 06:54:27
问题 When I compile the following code with the latest Visual Studio, it success to compile. class C; class T { public: template<typename A> void f(); private: C* c; }; int main() { T t; t.f<int>(); } template<typename A> void T::f() { this->c->g(); } class C { public: void g() {} }; But when I remove this-> from this->c->g() , compilation fails with C2027: use of undefined type 'C' . When I make the method f non-template, it fails to compile no matter this-> presents or not, so I think it's

What exactly is invalidation of reference/pointer?

本小妞迷上赌 提交于 2021-01-27 05:28:41
问题 I cannot find any definition for invalidation of pointers/references in the Standard. I ask because I just found out that C++11 forbids copy-on-write (COW) for strings. As far as I understand, if COW was applied then p would be still a valid pointer and r a valid reference after the following commands: std::string s("abc"); std::string s2(s); char * p = &(s2[0]); char & r = s2[0]; s2[1] = "B"; Just they would no longer point/refer to the first character of s2 , but merely to the first

Function argument binding rules for passing an array by reference vs passing pointer

∥☆過路亽.° 提交于 2021-01-27 04:33:27
问题 To prevent any confusion, I very much understand the difference between arrays and pointers, the concept of decay-to-pointer, and the concept of passing an array by reference in C++, etc. My question here is specifically about the rules used by the compiler to select a function from a set of function overload candidates, when one overload takes an array reference, and the other overload takes a pointer. For example, suppose we have: template <class T, std::size_t N> void foo(const T (&arr)[N]

Is `int` always signed?

时间秒杀一切 提交于 2021-01-27 04:17:04
问题 I always thought that in C, int stands for signed int ; but I have heard that this behavior is platform specific and in some platforms, int is unsigned by default. Is it true? What says the standard, and has it evolved over time? 回答1: You are quite right. As per C11 (the latest c standard), chapter §6.7.2 int , signed , or signed int is categorized as same type (type specifiers, to be exact). So, int is the same as signed int . Also, re-iterating the same, from chapter §6.2.5/P4 There are