language-lawyer

Is it guaranteed that C++ standard library containers call the replaceable new functions?

不问归期 提交于 2019-12-30 17:22:06
问题 If I replace all the operator new signatures that I can, at least on the implementations I have tested, I see that the standard containers call into my replaced versions to allocate memory. Is this guaranteed by the standard? That is, would it be illegal for an implementation to use an optimized version which didn't call my replacement functions for the memory underlying the standard containers? 回答1: The default allocator for allocator-aware containers such as std::vector<T> is std::allocator

Details in the process of constructing a std::thread object

无人久伴 提交于 2019-12-30 10:35:27
问题 I'm interested in (and confused about) the details of constructing a std::thread object. According to cppreference, both the thread function and all arguments are value-copied to some thread-accessible storage, and then invoke. 1) What exactly is this thread-accessible storage? Is it semantically equivalent to some kind of thread-local storage, and the variables are destructed after the thread function returned? 2) What is the value-category of the arguments when passed to the thread function

Details in the process of constructing a std::thread object

China☆狼群 提交于 2019-12-30 10:35:05
问题 I'm interested in (and confused about) the details of constructing a std::thread object. According to cppreference, both the thread function and all arguments are value-copied to some thread-accessible storage, and then invoke. 1) What exactly is this thread-accessible storage? Is it semantically equivalent to some kind of thread-local storage, and the variables are destructed after the thread function returned? 2) What is the value-category of the arguments when passed to the thread function

Why does not a template template parameter allow 'typename' after the parameter list

强颜欢笑 提交于 2019-12-30 09:45:08
问题 Template template typename? When using template template syntax as in template <template <typename> class T> , it is required to use the keyword class , as using typename gives an error along the lines of: error: template template parameter requires 'class' after the parameter list Everywhere else the keywords typename and class are interchangeable in the basic case of declaring a template parameter. You could argue that the requirement when using template template is a hint that you are

Is scanf guaranteed to not change the value on failure?

穿精又带淫゛_ 提交于 2019-12-30 09:28:32
问题 If a scanf family function fails to match the current specifier, is it permitted to write to the storage where it would have stored the value on success? On my system the following outputs 213 twice but is that guaranteed? The language in the standard (C99 or C11) does not seem to clearly specify that the original value should remain unchanged (whether it was indeterminate or not). #include <stdio.h> int main() { int d = 213; // matching failure sscanf("foo", "%d", &d); printf("%d\n", d); //

Access to elements of array of arrays using common iterator

允我心安 提交于 2019-12-30 08:13:28
问题 Is it undefined behaviour in C++ to access elements in adjacent arrays as in following code? #include <type_traits> #include <algorithm> #include <iterator> int main() { int a[10][10]; static_assert(std::is_standard_layout< decltype(a) >::value, "!"); std::fill(std::begin(*std::begin(a)), std::end(*std::prev(std::end(a))), 0); struct B { int b[10]; }; B b[10]; static_assert(std::is_standard_layout< decltype(b) >::value, "!"); std::fill(std::begin(std::begin(b)->b), std::end(std::prev(std::end

Why can't a class extend a static nested class occurring within it?

只愿长相守 提交于 2019-12-30 08:10:34
问题 This class: public class OuterChild extends OuterChild.InnerParent { public static class InnerParent { } } Fails to compile: $ javac OuterChild.java OuterChild.java:1: error: cyclic inheritance involving OuterChild public class OuterChild extends OuterChild.InnerParent { ^ 1 error because OuterChild would "depend on" itself, because (per §8.1.4 "Superclasses and Subclasses" of The Java Language Specification, Java SE 8 Edition) a class directly depends on any type that "is mentioned in [its]

Why does typeof only sometimes throw ReferenceError?

做~自己de王妃 提交于 2019-12-30 08:09:09
问题 In Chrome and Firefox, typeof foo evalulates to 'undefined' . But typeof (function() { return foo; })() throws an error: ReferenceError: foo is not defined This destroys the notions that I have of susbstitutability of expressions! Until now, I knew of no conditions for which foo and (function() { return foo; })() are not the same. Is this standard behavior? If so, it would be helpful to quote the relevant part of the ECMAScript standard. EDIT: Another example: typeof (foo) typeof (foo + 0) I

Is it legal to use side-effects in exceptions thrown by constexpr?

心不动则不痛 提交于 2019-12-30 07:58:10
问题 Normally, constexpr must be free of side-effects. However, I just discovered that it is possible to use side-effects in the constructors of thrown exceptions. That technique can be used to emulate assert() for constexpr functions, as it is demonstrated in the following program. #include <iostream> #include <cstdlib> #include <stdexcept> struct constexpr_precond_violated : std::logic_error { constexpr_precond_violated(const char* msg) : std::logic_error(msg) { std::cerr << msg << '\n'; abort()

Are compilers allowed to evaluate tautologies in static assert [duplicate]

ぐ巨炮叔叔 提交于 2019-12-30 06:17:11
问题 This question already has answers here : static_assert dependent on non-type template parameter (different behavior on gcc and clang) (2 answers) Closed 2 years ago . Providing a static_assert in templates are often helpful. In the case where a template shouldn't be instantiated in a certain way at all, I often do this template<typename T, typename = void> struct S { static_assert(false, "Unconditional error"); static_assert(sizeof(T) != sizeof(T), "Error on instantiation"); }; template