c++14

Devirtualizing a non-final method

一曲冷凌霜 提交于 2019-12-10 19:42:46
问题 Suppose I have a class setup like the following: class A { public: virtual void foo() { printf("default implementation\n"); } }; class B : public A { public: void foo() override { printf("B implementation\n"); } }; class C : public B { public: inline void foo() final { A::foo(); } }; int main(int argc, char **argv) { auto c = new C(); c->foo(); } In general, can the call to c->foo() be devirtualized and inlined down to the printf("default implementation") call? Is this guaranteed, for example

G++ doesn't permit use of protected default constructor in base class when both are templates?

你。 提交于 2019-12-10 19:26:11
问题 I've created a header for optionally-lazy parameters (also visible in a GitHub repository). In my original version of the code, I provided a protected default constructor for my base-class template: template <typename VAL_TYPE> class LazyType_Base { // .... LazyType_Base(void) =default; // .... Then, in one of the derived classes: template <typename VAL_TYPE> class LazyType_Eager : public LazyType_Base<VAL_TYPE> { public: LazyType_Eager( VAL_TYPE&& final_val) : LazyType_Base<VAL_TYPE>{} , val

How do I capture the results of a recursive function at compile-time?

你。 提交于 2019-12-10 18:23:38
问题 #include <iostream> template <typename T> struct node { T value; node const* prev; constexpr node(const T& value, node const* prev = nullptr) : value{value}, prev{prev} {} constexpr node push_front(const T& value) const { return node(value, this); } }; struct Something { node<int> n; constexpr Something(const int i) : n{node<int>(i)} {} constexpr Something(const node<int>& n) : n{n} {} }; constexpr void print(const Something& s) { bool first = true; for (const node<int>* i = &s.n; i !=

What are defaulted destructors used for?

为君一笑 提交于 2019-12-10 18:22:04
问题 I can understand defaulted constructors, since user defined constructors will disable the compiler generated ones making the object non trivially copyable etc. In the destructor case, apart from changing access category, what use is there to define a defaulted destructor considering that no user defined member function can disable them (you can't overload destructors anyway) ? // Which version should I choose ? struct Example { //1. ~Example() = default; //2. ~Example() {} //3. }; Even in the

how to forbid assignment to not reference variables?

一笑奈何 提交于 2019-12-10 18:09:40
问题 I fear it's a dumb question but... Someone can suggest me a way to force that a return value from a function (or a method), that return a reference to an internal static variable or a member of the class/struct, is assigned only to reference variables ? I try to explain what I desire with a minimal example. Given the following code, with a function wrapValue() that return a reference to the internal static variable, int & wrapValue (int v0) { static int val; return val = v0; } int main () { /

Define a template operator<< for iterables

北战南征 提交于 2019-12-10 17:53:12
问题 I'm able to define and use: std::ostream& operator<<(std::ostream& os, std::vector<int> const& container) { for (auto const& n : container) os << n << ", "; return os; } int main() { std::vector<int> data{0,1,2}; std::cout << data << '\n'; } (demo) But the definition of that operator doesn't depend on what kind of container I use. From there, I'd like to define a templated version: template<class Iterable> std::ostream& operator<<(std::ostream& os, Iterable const& iterable) { for (auto const&

Undefined behaviour of right shift (a >> b) when b is greater than the number of bits in a?

折月煮酒 提交于 2019-12-10 17:39:44
问题 Apparently, the behaviour of the right shift operation: a >> b is undefined in C and C++ when b >= sizeof(a)*CHAR_BIT (whereas in the normal case, the "new bits" introduced from the left due to the right shift are equal to zero). Why is this undefined behaviour better than setting the result to zero when b >= sizeof(a)*CHAR_BIT ? 回答1: We can get an idea of why languages choose undefined behavior from Why Language Designers Tolerate Undefined Behavior and it says: This answer came from two

Function returning auto with auto parameter munmap_chunk(): invalid pointer

左心房为你撑大大i 提交于 2019-12-10 17:10:01
问题 I'm testing out new feature for GCC 4.9 (auto in parameter) and getting some weird bug. #include <iostream> #include <vector> auto foo(auto v) { for (auto&& i : v) std::cout << i; } int main() { foo(std::vector<int>{1, 2, 3}); } This is giving me the following error: *** glibc detected *** ./a.out: munmap_chunk(): invalid pointer: 0x00007f87f58c6dc0 *** ======= Backtrace: ========= /lib/x86_64-linux-gnu/libc.so.6(+0x7e846)[0x7f87f4e4c846] ./a.out[0x400803] /lib/x86_64-linux-gnu/libc.so.6(_

call base class constructor without naming its class

送分小仙女□ 提交于 2019-12-10 16:44:16
问题 class MyDerived: public Incredble<Difficult< And<Complicated, Long>>, And<Even, Longer>, BaseClass, Name> { public: MyDerived(); } MyDerived::MyDerived : ???(params) {} Is there any way to call a base constructor without writing its full name and without typedeffing it? The reason is clearly to avoid code duplication and introducing multiple positions to change if a detail in the base class template params changes. Level 2 of this: template <uint32 C> class MyDerived: public Incredble

How to initialize a vector of unique_ptr with null pointers?

两盒软妹~` 提交于 2019-12-10 16:37:13
问题 I need to initialize a vector<unique<TNode>> with nullptr s. The method in this post is too complicated. My situation is special since I only need to initialize it as nullptr . How can I achieve it? I know I can use a for-loop to push_back a nullptr each time. Is there an elegant way? BTW, make_unqiue does not work on my compiler. #include <iostream> #include <memory> #include <vector> using namespace std; struct TNode { //char ch; bool isWord; vector<unique_ptr<TNode> > children; TNode():