language-lawyer

Template specialization doesn't see a function in its point of instantiation

拜拜、爱过 提交于 2020-01-03 16:52:31
问题 I don't understand why it isn't correct #include <iostream> using namespace std; struct CL{}; template <typename T> void fnc(T t) { f(t); } namespace NS { void f(CL){} void fn() {fnc(CL()); /*error is here*/} //point of instantiation fnc<CL> is here (in namespace scope, //according to 14.6.4.1/1) } int main(){} Calling f(t) in template function fnc is dependent on template parameter and then name lookup must be at an instantiation point. I saw Standard (C++ 14) 14.6.4.1/1 For a function

Can C++ compiler assume a const bool & value will not change?

放肆的年华 提交于 2020-01-03 16:39:36
问题 Can the C++ compiler assume a 'const bool &' value will not change? For example, imagine that I have a class: class test { public: test(const bool &state) : _test(state) { } void doSomething() { if (_test) { doMore(); } } void doMore(); private: const bool &_test; }; And I use it as follows: void example() { bool myState = true; test myTest(myState); while (someTest()) { myTest.doSomething(); myState = anotherTest(); } } Is it allowed by the standard for the compiler to assume _test's value

Can C++ compiler assume a const bool & value will not change?

大憨熊 提交于 2020-01-03 16:38:11
问题 Can the C++ compiler assume a 'const bool &' value will not change? For example, imagine that I have a class: class test { public: test(const bool &state) : _test(state) { } void doSomething() { if (_test) { doMore(); } } void doMore(); private: const bool &_test; }; And I use it as follows: void example() { bool myState = true; test myTest(myState); while (someTest()) { myTest.doSomething(); myState = anotherTest(); } } Is it allowed by the standard for the compiler to assume _test's value

Can C++ compiler assume a const bool & value will not change?

☆樱花仙子☆ 提交于 2020-01-03 16:38:08
问题 Can the C++ compiler assume a 'const bool &' value will not change? For example, imagine that I have a class: class test { public: test(const bool &state) : _test(state) { } void doSomething() { if (_test) { doMore(); } } void doMore(); private: const bool &_test; }; And I use it as follows: void example() { bool myState = true; test myTest(myState); while (someTest()) { myTest.doSomething(); myState = anotherTest(); } } Is it allowed by the standard for the compiler to assume _test's value

Can unsigned integer incrementation lead to undefined defined behavior?

点点圈 提交于 2020-01-03 16:00:55
问题 After reading the 32 bit unsigned multiply on 64 bit causing undefined behavior? question here on StackOverflow, I began to ponder whether typical arithmetic operations on small unsigned types could lead to undefined behavior according to the C99 standard. For example, take the following code: #include <limits.h> ... unsigned char x = UCHAR_MAX; unsigned char y = x + 1; The x variable is initialized to the maximum magnitude for the unsigned char data type. The next line is the issue: the

Can unsigned integer incrementation lead to undefined defined behavior?

倾然丶 夕夏残阳落幕 提交于 2020-01-03 16:00:49
问题 After reading the 32 bit unsigned multiply on 64 bit causing undefined behavior? question here on StackOverflow, I began to ponder whether typical arithmetic operations on small unsigned types could lead to undefined behavior according to the C99 standard. For example, take the following code: #include <limits.h> ... unsigned char x = UCHAR_MAX; unsigned char y = x + 1; The x variable is initialized to the maximum magnitude for the unsigned char data type. The next line is the issue: the

Is iteration order over the different Collection views of a given Map guaranteed to be consistent?

泪湿孤枕 提交于 2020-01-03 15:03:11
问题 For a given type of Map , are there any guarantees that iterating over the Collection views returned by the keySet , values and entries methods are iterated in the same order? Background: I'm wondering whether transforming public static void doSomethingForEachEntry(Map<String, Integer> someMap) { for (String key : someMap.keySet()) { doSomething(someMap.get(key)); } } to public static void doSomethingForEachEntry(Map<String, Integer> someMap) { for (Integer value : someMap.values()) {

Conversion operator in direct-initialization

久未见 提交于 2020-01-03 13:04:57
问题 The C++14 standard (N4296) says in 8.5/17.6.1 If the initialization is direct-initialization [...], constructors are considered. The applicable constructors are enumerated, and the best one is chosen through overload resolution. [...] If no constructor applies, or the overload resolution is ambiguous, the initialization is ill-formed. Therefore in direct-initialization, only constructors are considered - conversion functions are ignored. In the following code there is no applicable

Is it legal C++ to declare a nested namespace `std`?

ⅰ亾dé卋堺 提交于 2020-01-03 12:33:28
问题 The std namespace is special in C++, so ... Is this legal C++? // at global scope namespace mine { namespace std { ... } } I'd call it insane, but is it allowed? A reference (or non-reference) from the Standard would be appreciated. 回答1: In the reserved names standard 17.4.3.1 (and its sub-paragraphs) I can't find anything that prohibits using std as a nested namespace name. It's not a macro, it's not in the global namespace, and it doesn't seem to meet any of the "external linkage criteria"

Does std::move work with lvalue references? How does std::move work on standard containers?

我是研究僧i 提交于 2020-01-03 11:28:11
问题 #include <vector> struct A { int a[100]; }; void foo (const A& a) { std::vector<A> vA; vA.push_back(std::move(a)); // how does move really happen? } int main () { A a; foo(a); } The above code compiles fine. Now everywhere it's written that move avoids copying. Following are my queries: Does the move really work when one deals with a lvalue [non]- const reference? Even with "rvalue reference", how is the copy avoided when the object is inserted into a standard container like above? e.g. void