c++14

details of std::make_index_sequence and std::index_sequence

烂漫一生 提交于 2019-12-17 10:46:18
问题 I'm enjoying ramping up on variadic templates and have started fiddling about with this new feature. I'm trying to get my head around the implementation details of std::index_sequence 's (used for tuple implementation). I see sample code around there, but I really want a dumbed down step by step explanation of how an std::index_sequence is coded and the meta programming principal in question for each stage. Think really dumbed down :) 回答1: I see sample code around there, but I really want a

Is it defined behavior to reference an early member from a later member expression during aggregate initialization?

风流意气都作罢 提交于 2019-12-17 10:44:30
问题 Consider the following: struct mystruct { int i; int j; }; int main(int argc, char* argv[]) { mystruct foo{45, foo.i}; std::cout << foo.i << ", " << foo.j << std::endl; return 0; } Note the use of foo.i in the aggregate-initializer list. g++ 5.2.0 outputs 45, 45 Is this well-defined behavior? Is foo.i in this aggregate-initializer always guaranteed to refer to the being-created structure's i element (and &foo.i would refer to that memory address, for example)? If I add an explicit constructor

How to extract the source filename without path and suffix at compile time?

Deadly 提交于 2019-12-17 10:35:14
问题 Using both gcc with -std=c11 and g++ with -std=c++14. E.g. for a file named src/dir/Hello.cxx it should expand to something like e.g.: const char basename[] = "Hello"; or const char basename[] = getStaticBasename(__FILE__); as where getStaticBasename() is a macro (for C sources) or constexpr function (for C++ sources) which results to "Hello". I have to avoid splitting the string from __FILE__ at runtime, because the path and suffix must not be compiled into the executable in any way. The

Passing constexpr objects around

人盡茶涼 提交于 2019-12-17 10:01:27
问题 I decided to give then new C++14 definition of constexpr a spin and to get the most out of it I decided to write a little compile-time string parser. However, I'm struggling with keeping my object a constexpr while passing it to a function. Consider the following code: #include <cstddef> #include <stdexcept> class str_const { const char * const p_; const std::size_t sz_; public: template <std::size_t N> constexpr str_const( const char( & a )[ N ] ) : p_( a ), sz_( N - 1 ) {} constexpr char

Why was 1 << 31 changed to be implementation-defined in C++14?

谁说我不能喝 提交于 2019-12-17 07:23:50
问题 In all versions of C and C++ prior to 2014, writing 1 << (CHAR_BIT * sizeof(int) - 1) caused undefined behaviour, because left-shifting is defined as being equivalent to successive multiplication by 2 , and this shift causes signed integer overflow: The result of E1 << E2 is E1 left-shifted E2 bit positions; vacated bits are filled with zeros. [...] If E1 has a signed type and nonnegative value, and E1 × 2 E2 is representable in the result type, then that is the resulting value; otherwise,

Pointers in c++ after delete

对着背影说爱祢 提交于 2019-12-17 04:34:42
问题 After reading many posts about this, I want to clarify the next point: A* a = new A(); A* b = a; delete a; A* c = a; //illegal - I know it (in c++ 11) A* d = b; //I suppose it's legal, is it true? So the question is about using the value of copy of deleted pointer. I've read, that in c++ 11 reading the value of a leads to undefined behaviour - but what about reading the value of b ? Trying to read the value of the pointer (note: this is different to dereferencing it) causes implementation

C++11: Correct std::array initialization?

一笑奈何 提交于 2019-12-17 04:01:44
问题 If I initialize a std::array as follows, the compiler gives me a warning about missing braces std::array<int, 4> a = {1, 2, 3, 4}; This fixes the problem: std::array<int, 4> a = {{1, 2, 3, 4}}; This is the warning message: missing braces around initializer for 'std::array<int, 4u>::value_type [4] {aka int [4]}' [-Wmissing-braces] Is this just a bug in my version of gcc, or is it done intentionally? If so, why? 回答1: This is the bare implementation of std::array : template<typename T, std::size

Differences between std::make_unique and std::unique_ptr with new

回眸只為那壹抹淺笑 提交于 2019-12-16 22:45:08
问题 Does std::make_unique have any efficiency benefits like std::make_shared ? Compared to manually constructing std::unique_ptr : std::make_unique<int>(1); // vs std::unique_ptr<int>(new int(1)); 回答1: The motivation behind make_unique is primarily two-fold: make_unique is safe for creating temporaries, whereas with explicit use of new you have to remember the rule about not using unnamed temporaries. foo(make_unique<T>(), make_unique<U>()); // exception safe foo(unique_ptr<T>(new T()), unique

What are transparent comparators?

你说的曾经没有我的故事 提交于 2019-12-16 20:00:49
问题 In C++14, associative containers seem to have changed from C++11 – [associative.reqmts]/13 says: The member function templates find , count , lower_bound , upper_bound , and equal_range shall not participate in overload resolution unless the type Compare::is_transparent exists. What is the purpose of making an comparator "transparent"? C++14 also provides library templates like this: template <class T = void> struct less { constexpr bool operator()(const T& x, const T& y) const; typedef T

Code::Blocks error multiple definition of 'main'

夙愿已清 提交于 2019-12-14 04:12:19
问题 i tried to install my first program but it doesnt run. As the default program runs correctly. I created the new file as File>new>empty file and saved as .cpp but the program doesnt run. Ive matched the program from the book and the default one.. What is wrong 回答1: you can only have one main. By removing the Untitled.cpp file you will only have one file, main.cpp. you give your new file a name instead of taking the default name "Untitled.cpp". Or you can simply create an new project 来源: https: