language-lawyer

Can pointers to different types have different binary representations?

只愿长相守 提交于 2021-02-18 06:29:51
问题 I wonder if C++ implementations are allowed to represent pointers to different types differently. For instance, if we had 4-byte sized/aligned int and 8-byte sized/aligned long , would it be possible to represent pointers-to- int / long as object addresses shifted right by 2/3 bits, respectively? This would effectively forbid to convert a pointer-to- long into a pointer-to- int . I am asking because of [expr.reinterpret.cast/7]: An object pointer can be explicitly converted to an object

Why introduce `std::launder` rather than have the compiler take care of it?

孤人 提交于 2021-02-17 22:01:00
问题 I've just read What is the purpose of std::launder? and frankly, I am left scratching my head. Let's start with the second example in @NicolBolas' accepted answer: aligned_storage<sizeof(int), alignof(int)>::type data; new(&data) int; int *p = std::launder(reinterpret_cast<int*>(&data)); [basic.life]/8 tells us that, if you allocate a new object in the storage of the old one, you cannot access the new object through pointers to the old. std::launder allows us to side-step that. So, why not

Why doesn't the standard consider a template constructor as a copy constructor?

▼魔方 西西 提交于 2021-02-17 18:39:47
问题 Here's the definition of copy constructor, [class.copy.ctor/1]: A non-template constructor for class X is a copy constructor if its first parameter is of type X&, const X&, volatile X& or const volatile X&, and either there are no other parameters or else all other parameters have default arguments ([dcl.fct.default]). Why does the standard exclude templates as copy constructors? In this simple example, both constructors are copy constructors: struct Foo { Foo(const Foo &); // copy

Is there a difference when specifying upper bounds for wildcards explicitly?

醉酒当歌 提交于 2021-02-17 08:55:34
问题 Suppose I have a generic class Generic<A extends BaseType> . Is there a notable difference, as far as the Java Language Specification is concerned, between the following two type declarations? Generic<?> Generic<? extends BaseType> What about nested wildcards? List<Generic<?>> List<Generic<? extends BaseType>> Thinking about this, I would assume these to be equivalent. Generic specifies that the type parameter A has BaseType for an upper bound. Thus, the wildcard should always be

Does the JLS require inlining of final String constants?

别等时光非礼了梦想. 提交于 2021-02-16 09:57:11
问题 I ran into an issue while manipulating some bytecode, where a certain final String constant was not inlined by the java compiler (Java 8), see the example below: public class MyTest { private static final String ENABLED = "Y"; private static final String DISABLED = "N"; private static boolean isEnabled(String key) { return key.equals("A"); } private static String getString(String key, String value) { return key + value; } public static void main(String[] args) throws Exception { String flag =

Is it allowed to do longjmp() multiple times for one setjmp() call?

為{幸葍}努か 提交于 2021-02-10 03:26:14
问题 In my understanding, a typical usage of setjmp() and longjmp() is exception handling (usage in libpng should be a famous example of that) and there will be at most one call of longjmp() for one setjmp() call. Is it safely allowed to do longjmp() multiple times for one setjmp() call like this? #include <stdio.h> #include <setjmp.h> jmp_buf jb; int i; int main(void) { i = 0; setjmp(jb); printf("%d\n", i); i++; if (i < 10) longjmp(jb, 1); return 0; } Output: 0 1 2 3 4 5 6 7 8 9 I successfully

Is it allowed to do longjmp() multiple times for one setjmp() call?

╄→尐↘猪︶ㄣ 提交于 2021-02-10 03:23:32
问题 In my understanding, a typical usage of setjmp() and longjmp() is exception handling (usage in libpng should be a famous example of that) and there will be at most one call of longjmp() for one setjmp() call. Is it safely allowed to do longjmp() multiple times for one setjmp() call like this? #include <stdio.h> #include <setjmp.h> jmp_buf jb; int i; int main(void) { i = 0; setjmp(jb); printf("%d\n", i); i++; if (i < 10) longjmp(jb, 1); return 0; } Output: 0 1 2 3 4 5 6 7 8 9 I successfully

Name lookup after qualified declarator-id

久未见 提交于 2021-02-09 12:49:08
问题 Sec. 3.4.3/3 said: In a declaration in which the declarator-id is a qualified-id, names used before the qualified-id being declared are looked up in the defining namespace scope; names following the qualified-id are looked up in the scope of the member’s class or namespace. There is a code example from 3.4.3/3 N3797: class X { }; class C { class X { }; static const int number = 50; static X arr[number]; }; X C::arr[number];// ill-formed: // equivalent to: ::X C::arr[__C::number__]; // not to:

Name lookup after qualified declarator-id

[亡魂溺海] 提交于 2021-02-09 12:45:58
问题 Sec. 3.4.3/3 said: In a declaration in which the declarator-id is a qualified-id, names used before the qualified-id being declared are looked up in the defining namespace scope; names following the qualified-id are looked up in the scope of the member’s class or namespace. There is a code example from 3.4.3/3 N3797: class X { }; class C { class X { }; static const int number = 50; static X arr[number]; }; X C::arr[number];// ill-formed: // equivalent to: ::X C::arr[__C::number__]; // not to:

Name lookup after qualified declarator-id

て烟熏妆下的殇ゞ 提交于 2021-02-09 12:44:52
问题 Sec. 3.4.3/3 said: In a declaration in which the declarator-id is a qualified-id, names used before the qualified-id being declared are looked up in the defining namespace scope; names following the qualified-id are looked up in the scope of the member’s class or namespace. There is a code example from 3.4.3/3 N3797: class X { }; class C { class X { }; static const int number = 50; static X arr[number]; }; X C::arr[number];// ill-formed: // equivalent to: ::X C::arr[__C::number__]; // not to: