c++11

decltype( constexpr variable)

女生的网名这么多〃 提交于 2021-02-16 16:13:27
问题 Why decltype of constexpr variable is failed ? #include <cstdint> #include <type_traits> constexpr uint16_t foo(){ return 0;} constexpr auto cv = foo(); auto v = foo(); static_assert( std::is_same< uint16_t, decltype(cv)>::value, "!"); // failed static_assert( std::is_same< uint16_t, decltype(v) >::value, "!"); // success 回答1: decltype(entity) specifies the declared type of the entity specified by this expression. Due to the constexpr, ( A constexpr specifier used in an object declaration

decltype( constexpr variable)

依然范特西╮ 提交于 2021-02-16 16:13:00
问题 Why decltype of constexpr variable is failed ? #include <cstdint> #include <type_traits> constexpr uint16_t foo(){ return 0;} constexpr auto cv = foo(); auto v = foo(); static_assert( std::is_same< uint16_t, decltype(cv)>::value, "!"); // failed static_assert( std::is_same< uint16_t, decltype(v) >::value, "!"); // success 回答1: decltype(entity) specifies the declared type of the entity specified by this expression. Due to the constexpr, ( A constexpr specifier used in an object declaration

why does emplace_back need move constructor

我怕爱的太早我们不能终老 提交于 2021-02-16 15:46:24
问题 I have the following code: #include <string> #include <vector> #include <iostream> class Test final { public: Test(const std::string& s) : s_(s) { std::cout << "constructing: " << s_ << std::endl; } #ifdef NO_MOVE private: Test(const Test& t) = delete; Test(Test&& t) = delete; #else public: Test(const Test& t) : s_(t.s_) { std::cout << "copying: " << s_ << std::endl; }; Test(Test&& t) : s_(std::move(t.s_)) { std::cout << "moving: " << s_ << std::endl; }; #endif private: std::string s_; }; int

How to get IO error messages when creating a file in C++?

走远了吗. 提交于 2021-02-16 14:26:08
问题 One of the ancient anti-pattern is people checking error status and then returning fairly useless messages like "operation failed" instead of "operation failed because ...". I want C++ file I/O operations to fail with exception and get the error message on why it failed. Specifically I want ofstream object to raise exception when file creation fails and get bit more useful message such as "permission denied" or "No file or path". This is trivial to do in languages such as C# or Java or Python

creating an std::array with size calculated during run time

这一生的挚爱 提交于 2021-02-16 14:03:58
问题 I want to create an object of std::array<T, N> but the problem is I can only use functions that return a constexpr type or compiler will complain. The problem here is that I need to calculate the length of this array based on another array's size which could be something like this: template <typename T> struct DataLength { template <typename iter> size_t maxPossibleLength(iter begin, iter end) { size_t m_size = 0; while (begin != end) { m_size = m_size << 8 | std::numeric_limits<T>::max(); /*

How can you reserve space for an array without initializing each element?

送分小仙女□ 提交于 2021-02-16 13:40:09
问题 The following code will construct array of 10 Foo objects with default Foo constructor: Foo foo[10]; but i don't want do this, i have havy foo constructor and later i will regenarate all foo objects one by one and assign (copy) it to elements of foo array, so there is no sense to initialize array, i want just reserve space for it and set it elements later. Just like in the case of int foo[10] when elements of foo will not be initialized without ={}. How can i do this without using std

Why it is not possible to use an initializer_list to initialize a vector of unique_ptr's? [duplicate]

南笙酒味 提交于 2021-02-16 08:57:38
问题 This question already has answers here : initializer_list and move semantics (8 answers) Closed 6 years ago . I'm wondering why initializer_list doesn't work with unique_ptr: std::vector<std::unique_ptr<int>> vptr = {std::make_unique<int>(1), std::make_unique<int>(2)}; do not compile. However: std::vector<std::unique_ptr<int>> vptr(2); vptr[0] =std::make_unique<int>(1); vptr[1] =std::make_unique<int>(2); compile. and std::vector<int*>vint={new int{1},new int{2}}; or std::vector<std::shared

Why it is not possible to use an initializer_list to initialize a vector of unique_ptr's? [duplicate]

一笑奈何 提交于 2021-02-16 08:55:29
问题 This question already has answers here : initializer_list and move semantics (8 answers) Closed 6 years ago . I'm wondering why initializer_list doesn't work with unique_ptr: std::vector<std::unique_ptr<int>> vptr = {std::make_unique<int>(1), std::make_unique<int>(2)}; do not compile. However: std::vector<std::unique_ptr<int>> vptr(2); vptr[0] =std::make_unique<int>(1); vptr[1] =std::make_unique<int>(2); compile. and std::vector<int*>vint={new int{1},new int{2}}; or std::vector<std::shared

shrink_to_fit() vs swap trick

强颜欢笑 提交于 2021-02-16 05:54:14
问题 I have a game where certain game objects spawn all at once and then despawn as they get destroyed/killed. The game objects are elements in an std::vector , and I'd like to minimize memory usage. I'm used to the swap trick, std::vector<gameObject>(gameObjectVector.begin(), gameObjectVector.end()).swap(gameObjectVector); but I noticed the inbuilt shrink_to_fit() from C++11. However, it has linear complexity while the swap trick is constant. Isn't the swap trick superior in every way? 回答1: The

Ternary operator + C++11 constructor from initializer_list

泄露秘密 提交于 2021-02-16 05:39:11
问题 While developing an application, I had the following problem. I wanted to return an empty std::list<string> when a given function pointer was null, or the result of that function otherwise. This is a simplified version of my code: typedef std::list<std::string> (*ParamGenerator)(); std::list<std::string> foo() { /* ... */ ParamGenerator generator = ...; if(generator) return generator(); else return {}; } However, I usually like to use the ternary ( ?: ) operator in these cases, so I tried