c++17

Variable templates and std::cout — order of construction

不问归期 提交于 2019-12-10 01:04:43
问题 It looks like we can safely use std::cout object in constructors of objects with static storage duration as stated in this question. However, I'm not entirely sure that we can safely use them in case of variable templates: #include <iostream> template<class T> T x = T{}; void foo() { class Test { public: Test() { std::cout << "Test::Test\n"; } }; Test t = x<Test>; } int main() { std::cout << "main\n"; } This code crashes in clang (live example) and I'm not sure whether it's a bug or not. 回答1:

Initialize static std::map with non copyable value in a uniformed inline initialization

时光总嘲笑我的痴心妄想 提交于 2019-12-09 17:07:03
问题 I'd like to initialize a static std::map where the value is not copyable. I'll call my class ValueClass . ValueClass has an std::unique_ptr as private member and I even ensure that ValueClass is not copyable by extending non_copyable that looks like the following: class non_copyable { public: non_copyable() = default; protected: virtual ~non_copyable() = default; private: non_copyable(const non_copyable&) = delete; non_copyable& operator=(const non_copyable&) = delete; }; Now I'm trying to

How does std::launder affect containers?

两盒软妹~` 提交于 2019-12-09 15:10:34
问题 Consider the following, simplified and incomplete, implementation of a fixed-sized vector: template<typename T> class Vec { T *start, *end; public: T& operator[](ssize_t idx) { return start[idx]; } void pop() { end--; end->~T(); } template<typename... U> void push(U... args) { new (end) T { std::forward<U>(args)... }; end++; } }; Now consider the following T: struct T { const int i; }; And the following use case: Vec<T> v; v.push(1); std::cout << v[0].i; v.pop(); v.push(2); std::cout << v[0]

How to provide deduction guide for nested template class?

让人想犯罪 __ 提交于 2019-12-09 14:17:41
问题 According to [temp.deduct.guide/3]: (...) A deduction-guide shall be declared in the same scope as the corresponding class template and, for a member class template, with the same access. (...) But below example doesn't seem to compile in both [gcc] and [clang]. #include <string> template <class> struct Foo { template <class T> struct Bar { Bar(T) { } }; Bar(char const*) -> Bar<std::string>; }; int main() { Foo<int>::Bar bar("abc"); static_cast<void>(bar); } What is the correct syntax of

Why is the const&& overload of as_const deleted?

烂漫一生 提交于 2019-12-09 14:07:44
问题 On a blog on the progress of C++17 I read the following: P0007 proposes a helper function template as_const , which simply takes a reference and returns it as a reference to const . template <typename T> std::add_const_t<T>& as_const(T& t) { return t } template <typename T> void as_const(T const&&) = delete; Why is the const&& overload deleted? 回答1: Consider what would happen if you didn't have that overload, and try to pass in a const rvalue: template <typename T> const T &as_const(T &t) {

std::optional implemented as union vs char[]/aligned_storage

时间秒杀一切 提交于 2019-12-09 09:46:24
问题 While reading through GCC's implementation of std::optional I noticed something interesting. I know boost::optional is implemented as follows: template <typename T> class optional { // ... private: bool has_value_; aligned_storage<T, /* ... */> storage_; } But then both libstdc++ and libc++ (and Abseil ) implement their optional types like this: template <typename T> class optional { // ... private: struct empty_byte {}; union { empty_byte empty_; T value_; }; bool has_value_; } They look to

Is it legal to check whether the address of a subobject lies within the bounds of a containing object

我的未来我决定 提交于 2019-12-09 08:41:06
问题 2 Questions: Is the following code well formed with defined behaviour? Is there any possible c++ implementation in which it could assert? Code (c++11 and higher): #include <cassert> #include <utility> #include <ciso646> template<class T> auto to_address(T* p) { return reinterpret_cast<unsigned char const*>(p); } /// Test whether part is a sub-object of object template<class Object, class Part> bool is_within_object(Object& object, Part& part) { auto first = to_address(std::addressof(object)),

Ambiguity error in C++17 (template template parameters and default arguments issue)

老子叫甜甜 提交于 2019-12-09 07:39:38
问题 I have code which is differently interpreted by g++ with the c++14 and c++17 standard flags: #include <iostream> #include <vector> template<class T, class A> void func(const std::vector<T, A>&v) { std::cout << 1 << std::endl; } template<typename T, template <typename>class Vector> void func(const Vector<T>&v) { std::cout << 2 << std::endl; } void f() { std::vector<int> v; func(v); } int main() { f(); return 0; } When I'm trying compile this code with command g++ -std=c++14 -Wall -pedantic

Where to use std::variant over union?

久未见 提交于 2019-12-09 07:30:34
问题 Please explain what is the difference between union and std::variant and why std::variant was introduced into the standard? In what situations should we use std::variant over the old-school union ? 回答1: Generally speaking, you should prefer variant unless one of the following comes up: You're cheating. You're doing type-punning or other things that are UB but you're hoping your compiler won't break your code. You're doing some of the pseudo-punnery that C++ union s are allowed to do:

Cv-qualifications of prvalues (revisited)

旧城冷巷雨未停 提交于 2019-12-09 05:06:07
问题 This is a followup to my previous question, where the apparent consensus was that the change in treatment of cv-qualifications of prvalues was just a fairly minor and inconsequential change intended to solve some inconsistencies (e.g. functions returning prvalues and declared with cv-qualified return types). However, I see another place in the standard that appears to rely on prvalues having cv-qualified types: initialization of const references with prvalues through temporary materialization