language-lawyer

A using-declaration can not be repeated in function scope. Why is that?

℡╲_俬逩灬. 提交于 2019-12-31 19:26:30
问题 In [namespace.udecl]/10 you have the following example: namespace A { int i; } namespace A1 { using A::i; using A::i; // OK: double declaration } void f() { using A::i; using A::i; // error: double declaration } This snippet compiles in clang. 回答1: The first is a declaration inside a namespace, and the multiple using statements could happen frequently using #includes. The second is inside a definition of a function, and you would never do that unless you made a mistake. You can't define the

A using-declaration can not be repeated in function scope. Why is that?

别等时光非礼了梦想. 提交于 2019-12-31 19:23:51
问题 In [namespace.udecl]/10 you have the following example: namespace A { int i; } namespace A1 { using A::i; using A::i; // OK: double declaration } void f() { using A::i; using A::i; // error: double declaration } This snippet compiles in clang. 回答1: The first is a declaration inside a namespace, and the multiple using statements could happen frequently using #includes. The second is inside a definition of a function, and you would never do that unless you made a mistake. You can't define the

Calling non-static member function outside of object's lifetime in C++17

谁说胖子不能爱 提交于 2019-12-31 11:24:04
问题 Does the following program have undefined behavior in C++17 and later? struct A { void f(int) { /* Assume there is no access to *this here */ } }; int main() { auto a = new A; a->f((a->~A(), 0)); } C++17 guarantees that a->f is evaluated to the member function of the A object before the call's argument is evaluated. Therefore the indirection from -> is well-defined. But before the function call is entered, the argument is evaluated and ends the lifetime of the A object (see however the edits

Calling non-static member function outside of object's lifetime in C++17

早过忘川 提交于 2019-12-31 11:22:27
问题 Does the following program have undefined behavior in C++17 and later? struct A { void f(int) { /* Assume there is no access to *this here */ } }; int main() { auto a = new A; a->f((a->~A(), 0)); } C++17 guarantees that a->f is evaluated to the member function of the A object before the call's argument is evaluated. Therefore the indirection from -> is well-defined. But before the function call is entered, the argument is evaluated and ends the lifetime of the A object (see however the edits

Does specifying constexpr on constructor automatically makes all objects created from it to be constexpr?

冷暖自知 提交于 2019-12-31 09:08:07
问题 Here is my code: class test{ public: constexpr test(){ } constexpr int operator+(const test& rhs){ return 1; } }; int main(){ test t; //constexpr word isn't necessary constexpr int b = t+test(); // works at compile time! int w = 10; // ERROR constexpr required constexpr int c = w + 2; // Requires w to be constexpr return 0; } I notice that it worked even though I didn't specify test to be constexpr . I tried replicating the result by doing the same with int but i get errors. Specifically, it

Is std::move really needed on initialization list of constructor for heavy members passed by value?

我与影子孤独终老i 提交于 2019-12-31 08:36:09
问题 Recently I read an example from cppreference.../vector/emplace_back: struct President { std::string name; std::string country; int year; President(std::string p_name, std::string p_country, int p_year) : name(std::move(p_name)), country(std::move(p_country)), year(p_year) { std::cout << "I am being constructed.\n"; } My question: is this std::move really needed? My point is that this p_name is not used in the body of constructor, so, maybe, there is some rule in the language to use move

Constructor with non-type template arguments

╄→尐↘猪︶ㄣ 提交于 2019-12-31 03:17:14
问题 In this question it's stated that it's impossible to just directly use template arguments for class constructor, because if you write something like struct S{ template<typename T> S() { ... } } Then you have no way of calling this constructor. However, there're some workarounds to make this work, for example, through template argument deduction. But all of these workarounds I know are for type arguments only. So, the question is Are there any workarounds to make this work for non-type

c++ only: unary minus for 0x80000000

限于喜欢 提交于 2019-12-31 02:44:06
问题 This question is supposedly for language-lawyers. Suppose that signed and unsigned int are both 32 bits wide. As stated in the n3337.pdf draft, 5.3.1.8, (-(0x80000000u)) = 0x100000000u-0x80000000u = 0x80000000u But I can not find the answer to the question: what will be unary minus for signed 0x80000000? Is it UB, implementation defined, or ... ? The question is mostly about run-time calculation. Say signed int my_minus(signed int i) { return -i;} .... int main() { signed int a = -0x7FFFFFFF;

Is printf()'s string width safe with unterminated strings?

谁说我不能喝 提交于 2019-12-30 22:59:13
问题 Is the following well defined? const char not_a_c_string[] = { 'h', 'e', 'l', 'l', 'o' }; printf( "%.5s", (const char*) not_a_c_string ); This is a question about the specific form "%.5s" , and not an how to print a possibly not NUL-terminated string? as this question has already been answered here where the "%.*s" construct is suggested. 回答1: First of all, I believe, you meant to ask about the precision , not the field width . So, your example is to look like printf( "%.5s", (const char*)

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

て烟熏妆下的殇ゞ 提交于 2019-12-30 17:22:35
问题 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