c++14

Practical difference between a struct with only operator() and a normal function

隐身守侯 提交于 2020-08-02 07:50:55
问题 I have seen code that looks like this: struct foo_functor { template <typename T, typename U> constexpr auto operator()(T t, U u) const -> decltype(t | u) { return t | u; } }; constexpr foo_functor foo; As far as I can tell, it's the same as the following: template <typename T, typename U> constexpr auto foo(T t, U u) -> decltype(t | u) { return t | u; } Why would you want to do the first one? Are there any differences? As far as I could see from the compiler output, at least with constexpr ,

Why does GCC's threading standard library implementation throw exceptions if you don't include pthread?

a 夏天 提交于 2020-07-19 03:43:11
问题 When I write code that uses, for example, std::promise , and I don't include the PThread library in GCC, I get an exception thrown rather than a linker error. For example: void product(std::promise<int> intPromise, int a, int b) { intPromise.set_value(a * b); } int main() { int a = 20; int b = 10; std::promise<int> prodPromise; std::future<int> prodResult = prodPromise.get_future(); product(std::move(prodPromise), a, b); std::cout << "20*10= " << prodResult.get() << std::endl; } If I compile

What is const void?

不打扰是莪最后的温柔 提交于 2020-07-16 16:12:31
问题 The description of std::is_void states that: Provides the member constant value that is equal to true, if T is the type void, const void, volatile void, or const volatile void. Then what could be const void , or a volatile void ? This answer states that const void return type would be invalid (however compiles on VC++ 2015) const void foo() { } If by standard, const void is invalid (VC being wrong) - then what is const void ? 回答1: const void is a type which you can form a pointer to. It's

Preincrement vs postincrement in terms of sequence points

久未见 提交于 2020-07-09 02:34:39
问题 In this answer there're some examples of well-defined and undefined expressions. I'm particularly interested in two of them: (6) i = i++ + 1; // Undefined Behaviour (7) i = ++i + 1; // Well-defined Behaviour This means that there's a difference between pre-increment and post-increment in terms of sequence points and well defined /unspecified/undefined behavior, but I don't understand where this difference comes from. In standard draft ( N4618 ) there's an example of code ([intro.execution],

Spliting component in Entity-Component-System demands too much refactoring

ε祈祈猫儿з 提交于 2020-07-08 05:56:17
问题 I have an existing working C++ game library that use Entity-Component-System (ECS). User of my library would like to create some components e.g. Cat :- class Cat{ public: int hp; float flyPower; }; He can modify hp of every cat by e.g. :- for(SmartComponentPtr<Cat> cat : getAll<Cat>()){ cat->hp-=5; //#1 } Some days later, he want to split Cat to HP and Flyable :- class HP{ public: int hp; }; class Flyable{ public: float flyPower; }; Thus, every cat that access hp will compile error (e.g. at

Spliting component in Entity-Component-System demands too much refactoring

老子叫甜甜 提交于 2020-07-08 05:55:11
问题 I have an existing working C++ game library that use Entity-Component-System (ECS). User of my library would like to create some components e.g. Cat :- class Cat{ public: int hp; float flyPower; }; He can modify hp of every cat by e.g. :- for(SmartComponentPtr<Cat> cat : getAll<Cat>()){ cat->hp-=5; //#1 } Some days later, he want to split Cat to HP and Flyable :- class HP{ public: int hp; }; class Flyable{ public: float flyPower; }; Thus, every cat that access hp will compile error (e.g. at

Spliting component in Entity-Component-System demands too much refactoring

◇◆丶佛笑我妖孽 提交于 2020-07-08 05:53:10
问题 I have an existing working C++ game library that use Entity-Component-System (ECS). User of my library would like to create some components e.g. Cat :- class Cat{ public: int hp; float flyPower; }; He can modify hp of every cat by e.g. :- for(SmartComponentPtr<Cat> cat : getAll<Cat>()){ cat->hp-=5; //#1 } Some days later, he want to split Cat to HP and Flyable :- class HP{ public: int hp; }; class Flyable{ public: float flyPower; }; Thus, every cat that access hp will compile error (e.g. at

Storing an std::thread in C++11 smart pointer

拥有回忆 提交于 2020-07-05 07:57:13
问题 In C++ 11 & above what are the advantages or disadvantages when storing an std::thread as a member of class directly like so: std::thread my_thread; As opposed to storing a std::shared_ptr or std::unique_ptr to the thread like so: std::shared_ptr<std::thread> my_thread_ptr; Is any of the code options better than other? Or it doesn't matter, just 2 separate ways of handling the thread object. 回答1: May be there is some less common reason for usage of pointer (or smart pointer) member but for

Reassign unique_ptr object with make_unique statements - Memory leak?

和自甴很熟 提交于 2020-07-04 22:01:49
问题 I don't get what following statement would do (specially second line)? auto buff = std::make_unique<int[]>(128); buff = std::make_unique<int[]>(512); Will the second call to make_unique followed by assignment operator will de-allocate memory allocated by first call, or will there be memory leak? Must I have to use buff.reset(new int[512]); ? I've debugged it, but didn't find any operator= being called, nor any destructor be invoked (by unique_ptr ). 回答1: gcc 5.3: #include <memory> extern void

How do I make template type deduction work with references?

一世执手 提交于 2020-07-03 02:38:35
问题 I have a template function, func : template<typename T> void func(T p) { f(p); } And a set of functions f : f(SomeType&); f(int); ... If I instantiate the template function, func , using a reference as function argument p , without explicitly specifying template parameter T , then the type deduced will not be the reference type of p , but rather the type p is a reference to, for example: SomeType s; SomeType& ref_to_s = s; func(ref_to_s); // Type deduction results in: func<SomeType>(ref_to_s)