- Available C++ Libraries FAQ - 1 Update
- brace initialization of subclasse of array - 7 Updates
- Calling virtual function from base class constructor - 1 Update
- alignment and padding wrt cache lines... - 1 Update
Nikki Locke <nikki@trumphurst.com>: Sep 14 10:23PM Available C++ Libraries FAQ URL: http://www.trumphurst.com/cpplibs/ This is a searchable list of libraries and utilities (both free and commercial) available to C++ programmers. If you know of a library which is not in the list, why not fill in the form at http://www.trumphurst.com/cpplibs/cppsub.php Maintainer: Nikki Locke - if you wish to contact me, please use the form on the website. |
Ralf Goertz <me@myprovider.invalid>: Sep 14 10:18AM +0200 Hi, why does brace initialization of a subclass of array fail and how do I fix this? #include <array> //typedef std::array<double,3> Space; //with this line it works struct Space : public std::array<double,3>{}; int main() { Space x={{1,2,3}}; //fails when Space is subclass of array return 0; } |
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Sep 14 10:48AM +0200 On 9/14/2017 10:18 AM, Ralf Goertz wrote: > Space x={{1,2,3}}; //fails when Space is subclass of array > return 0; > } It's no longer an aggregate (simple class with no bases or user-provided constructors). List-initialization reduces to aggregate initialization when the class is an aggregate. Otherwise various special cases are considered, e.g. initializing a `std::initializer_list`, and in your case constructors are considered, and none were found. You can simply provide a constructor that takes a `std::initializer_list`. `std::array` stems from Boost, from the time before `std::initializer_list`, and was designed for old C++ and C aggregate initialization. Cheers & hth., - Alf |
Ralf Goertz <me@myprovider.invalid>: Sep 14 12:21PM +0200 Am Thu, 14 Sep 2017 10:48:19 +0200 > It's no longer an aggregate (simple class with no bases or > user-provided constructors). List-initialization reduces to aggregate > initialization when the class is an aggregate. Hm, you mean to say it works with array because it doesn't have a user-provided constructor so it reduces to aggregate initialization? Which then fails in a subclass because it is no longer aggregate? But the above program also fails to compile when I substitute array with vector (but still works with the typedef). And vector does have non-default constructors hence it is not aggregate. So why doesn't a subclass of vector inherit its list-initialization constructors? |
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Sep 14 04:14PM +0200 On 9/14/2017 12:21 PM, Ralf Goertz wrote: > vector (but still works with the typedef). And vector does have > non-default constructors hence it is not aggregate. So why doesn't a > subclass of vector inherit its list-initialization constructors? If you want constructor inheritance then you have to specify that explicitly, e.g. (off the cuff) struct Foo : std::vector<int> { using std::vector<int>::vector<int>; // Inherit constrcts }; Cheers & hth., - Alf |
Ralf Goertz <me@myprovider.invalid>: Sep 14 04:43PM +0200 Am Thu, 14 Sep 2017 16:14:31 +0200 > If you want constructor inheritance then you have to specify that > explicitly, e.g. (off the cuff) That cuff needs a link ;-) > : std::vector<int> > { > using std::vector<int>::vector<int>; // Inherit constrcts error: a template-id may not appear in a using-declaration > }; I don't seem to be able to fix that though. :-( |
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Sep 14 06:14PM +0200 On 9/14/2017 4:43 PM, Ralf Goertz wrote: > error: a template-id may not appear in a using-declaration >> }; > I don't seem to be able to fix that though. :-( Oh sorry. #include <vector> struct Foo : std::vector<int> { using std::vector<int>::vector; }; auto main() -> int { Foo const v{ 1, 2, 3 }; } There. Another way is to define a name for the base class. Cheers & hth., - Alf |
asetofsymbols@gmail.com: Sep 14 12:30PM -0700 Using std::ftrytr:: Etc As the line you wrote it is for me horrible There would be some other way to access in namespace |
Juha Nieminen <nospam@thanks.invalid>: Sep 13 07:06AM >>by having to explicitly call an "init()" function for each object >>that's created. > Yes, of course. How is that a problem? It transfers initialization duties to the calling code, which is very bad object-oriented design. Not significantly better than transferring freeing duties to the calling code (which is very much against good OO design and the RAII principle). It's very easy to forget to call the init() function in many situations (in the same way as it would be easy to eg. call 'delete' to destroy the object.) |
"Chris M. Thomasson" <invalid@invalid.invalid>: Sep 13 07:38PM -0700 On 9/10/2017 2:42 PM, Ian Collins wrote: >>
Subscribe to:
Post Comments (Atom)
|
No comments:
Post a Comment