inherited-constructors

c++0x inherited constructor in templates

拜拜、爱过 提交于 2020-01-11 03:06:10
问题 Here is class foo: template <typename T> struct foo { foo() { t = nullptr; } foo(T* p, bool flag) { t = p; } private: T* t; }; Here is class bar: template <typename T> struct bar: public foo<T> { using foo<T>::foo<T>; }; Is it correct syntax for inheriting constructors? If I use "using foo::foo;" then compiler of Visual C++ 2010 dies. So basically how to inherit constructors from template classes in VC++ 2010? 回答1: template <typename T> struct bar: public foo<T> { using foo<T>::foo<T>; }; To

Inherited constructors, default constructor and visibility

老子叫甜甜 提交于 2019-12-23 07:49:10
问题 As stated by [namespace.udecl]/18: [...] A using-declaration that names a constructor does not create a synonym; instead, the additional constructors are accessible if they would be accessible when used to construct an object of the corresponding base class, and the accessibility of the using-declaration is ignored. [...] Because of that, the following code does not compile: class B { protected: B(int) { } }; class D: B { using B::B; }; int main () { D d{0}; } It returns an error that is more

Need an example showing that default constructor is not inherited

荒凉一梦 提交于 2019-12-07 04:59:14
问题 I know that default constructor is not inherited, as stated in n3337. And there is an example there: struct B2 { B2(int = 13, int = 42); }; struct D2 : B2 { using B2::B2; }; With quite good explanation: The candidate set of inherited constructors in D2 for B2 is ... —B2(int = 13, int = 42) —B2(int = 13) —B2() And most important: The set of constructors present in D2 is —D2() , implicitly-declared default constructor, not inherited For me this example does not show the difference, in a sense

Should args to inherited constructors be copied when invoking the base ctor or not?

陌路散爱 提交于 2019-12-05 02:52:58
问题 For the following program: #include <iostream> struct Foo { Foo() { std::cout << "Foo()\n"; } Foo(const Foo&) { std::cout << "Foo(const Foo&)\n"; } ~Foo() { std::cout << "~Foo()\n"; } }; struct A { A(Foo) {} }; struct B : A { using A::A; }; int main() { Foo f; B b(f); } GCC gives: $ g++ -std=c++17 -O2 -Wall -pedantic -pthread main.cpp && ./a.out Foo() Foo(const Foo&) ~Foo() ~Foo() VS 2017 (also in C++17 mode) gives: Foo() Foo(const Foo&) Foo(const Foo&) ~Foo() ~Foo() ~Foo() Who's right, and

c++0x inherited constructor in templates

别说谁变了你拦得住时间么 提交于 2019-11-30 21:01:02
Here is class foo: template <typename T> struct foo { foo() { t = nullptr; } foo(T* p, bool flag) { t = p; } private: T* t; }; Here is class bar: template <typename T> struct bar: public foo<T> { using foo<T>::foo<T>; }; Is it correct syntax for inheriting constructors? If I use "using foo::foo;" then compiler of Visual C++ 2010 dies. So basically how to inherit constructors from template classes in VC++ 2010? template <typename T> struct bar: public foo<T> { using foo<T>::foo<T>; }; To let this parse correctly, you would need to insert template before the foo<T>; , to tell the compiler that

Inheriting constructors and brace-or-equal initializers

↘锁芯ラ 提交于 2019-11-27 15:33:45
I don't understand why you can't compile a class which has both a member (not default constructible) with a brace-or-equal initializer and an inherited constructor. g++ says : test.cpp:22:15: error: use of deleted function ‘Derived::Derived(float)’ Derived d(1.2f); test.cpp:16:13: note: ‘Derived::Derived(float)’ is implicitly deleted because the default definition would be ill-formed: using Base::Base; test.cpp:16:13: error: no matching function for call to ‘NoDefCTor::NoDefCTor()’ test.cpp:5:1: note: candidate: NoDefCTor::NoDefCTor(int) NoDefCTor(int) {} Code that fails to compile (under g++

Inheriting constructors and brace-or-equal initializers

独自空忆成欢 提交于 2019-11-26 18:31:38
问题 I don't understand why you can't compile a class which has both a member (not default constructible) with a brace-or-equal initializer and an inherited constructor. g++ says : test.cpp:22:15: error: use of deleted function ‘Derived::Derived(float)’ Derived d(1.2f); test.cpp:16:13: note: ‘Derived::Derived(float)’ is implicitly deleted because the default definition would be ill-formed: using Base::Base; test.cpp:16:13: error: no matching function for call to ‘NoDefCTor::NoDefCTor()’ test.cpp:5