c++14

Why are static members of template classes not unique

倖福魔咒の 提交于 2019-12-23 12:18:54
问题 Take a look at the following code: #include <iostream> template <typename T> class Foo { public: static T bar; }; template <typename T> typename T Foo<T>::bar; int main() { std::cout << "Foo<int>::bar : " << &Foo<int>::bar << std::endl; std::cout << "Foo<double>::bar : " << &Foo<double>::bar << std::endl; return 0; } This will print out 2 different addresses. I can understand why in this case, bar is of type T and thus instantiations of different T's in Foo<T> will get you different static

Is it valid to pass non-arithmetic types as arguments to cmath functions?

試著忘記壹切 提交于 2019-12-23 11:59:13
问题 Given the following user-defined type S with a conversion function to double : struct S { operator double() { return 1.0;} }; and the following calls to cmath functions using the type S : #include <cmath> void test(S s) { std::sqrt(s); std::log(s); std::isgreater(s,1.0); std::isless(s,1.0); std::isfinite(s) ; } This code compiles with gcc using libstdc++ ( see it live ) but with clang using libc++ it generates errors for several of the calls ( see it live ) with the following error for

Can arrays be indexed at compile time?

∥☆過路亽.° 提交于 2019-12-23 09:49:41
问题 In this comment to another question, the user hvd stated the following: ... although string literals can be passed to constexpr functions, and array indexing is allowed on string literals in constant expressions, an indexing operation on a constexpr function parameter doesn't qualify as a constant expression. I didn't fully understand what was meant. Does it mean that the hash_value variable in the following code #include <cstddef> // Compute the hash of a string literal adding the values of

Why does not std::nullptr_t work with std::cout in C++?

早过忘川 提交于 2019-12-23 09:39:28
问题 I learned about std::nullptr_t that is the type of the null pointer literal, nullptr . Then I made small program : #include <iostream> int main() { std::nullptr_t n1; std::cout<<n1<<endl; return 0; } Here, nullptr_t is data type and n1 is variable and I'm trying to print the value of variable. But, Compiler give an error: prog.cpp: In function 'int main()': prog.cpp:6:11: error: ambiguous overload for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'std:

Template instantiation, two-phase name lookup, different behavior with automatic deduced type

瘦欲@ 提交于 2019-12-23 09:19:49
问题 After seeing this question When is a C++ template instantiation type checked? , and wondering myself for quite some time the same thing I started to play with code to assimilate the knowledge. The answer gives clear and correct explanation. It mentions two-phase name lookup and the fact that the end of translation unit is also considered a point of instantiation for function templates. However I observed different behavior when using automatic return type deduction: This is like the original

Transitioning away from std::string, std::ostream, etc. in a library's public API

狂风中的少年 提交于 2019-12-23 09:00:25
问题 For API/ABI compatibility across many toolchains with the same binary, it is well known that STL containers, std::string , and other standard library classes like iostreams are verboten in public headers. (Exceptions to this are if one is distributing one build for each version of supported toolchains; one delivers source with no binaries for end-user compilation, which are not preferred options in the present case; or one translates to some other container inline so that a differing std

Are digit separators allowed before the digits in a hex or binary number?

你说的曾经没有我的故事 提交于 2019-12-23 08:54:43
问题 C++14 introduced the concept of digit separators into literals, along the lines of 3'141'592'653'589 . Now this is a great feature for readable code but I was wondering whether it allowed quotes before the numeric portion of a 0x/0b -type literal. It seems to me that: unsigned int topThreeBits = 0b'1110'0000; unsigned int hexNum = 0x'dead'beef; is more readable than the one without a leading separator: unsigned int topThreeBits = 0b1110'0000; unsigned int hexNum = 0xdead'beef; because it

How to implement thread-safe container with natural looking syntax?

痴心易碎 提交于 2019-12-23 08:34:05
问题 Preface Below code results in undefined behaviour, if used as is: vector<int> vi; ... vi.push_back(1); // thread-1 ... vi.pop(); // thread-2 Traditional approach is to fix it with std::mutex : std::lock_guard<std::mutex> lock(some_mutex_specifically_for_vi); vi.push_back(1); However, as the code grows, such things start looking cumbersome, as everytime there will be a lock before a method. Moreover, for every object, we may have to maintain a mutex. Objective Without compromising in the

Why no transparent C++1x std::map::at?

天大地大妈咪最大 提交于 2019-12-23 07:57:46
问题 Is there a reason for missing transparent ( template <class K> at(K&& key); ) in std::map ? 回答1: My guess is that std::map::at() must be a "bounds-checked" version of std::map::operator[]() . Providing a transparent version of std::map::operator[]() imposes an additional requirement on std::map::key_type and the query key type K - if the query key is not in the map, it must be inserted (with default constructed value), which means that std::map::key_type must be constructible from the the

What's the most C++ way to check if value belongs to certain static set?

别来无恙 提交于 2019-12-23 07:56:06
问题 Let's say that I want to write something like this (the {1, 3, 7, 42, 69, 550123} set is known before compilation): int x; ... if (x == 1 || x == 3 || x == 7 || x == 42 || x == 69 || x == 5550123) { ... } The condition looks ugly because we have 9 extra symbols (" || x == ") for each possible value. How can I rewrite it in a more C++ way? My best guess is: int x; ... const std::unordered_set<int> v = {1, 3, 7, 42, 69, 5550123}; if (v.count(x)) { ... } It has O(1) average complexity with some