A native wrapper, allowing some missing functionality.

As you probably know 'C++' doesn't allow either passing or returning arrays by value (don't mislead yourself that declaring array parameters will give you that functionality, every parameter declared as array will be 'adjusted' to a pointer to it's first element - which is some kind of odd, but 'C++' holds big legacy). You can use "std::array" for the case but as it have some non-inline methods it will require external linkage and slow-down your program. My wrapper on the other hand is fully inline and will be expanded all in compile-time so heavy optimizations can be performed. As I said it allow passing native arrays and returning them by value. It also allow assigning arrays and some-time in the future I could add addition of them too. The wrapper also allow creation of unnamed and uninitialized temporaries. The wrapper:

It also have another, more complex usage - declaring unnamed and uninitialized temporaries. This is when you don't care for the output of some function. Example you have:

void FuncDoSomethingAndStoreResult(int (&refArrayOutput)[260]);

This function will do something and store some output in 'refArrayOutput', but sometimes you wouldn't care for it and you'll only want the function to do something for you. For now this was only possible by using default 'zero-initialization', which however is a performance waste. With my wraper you can do that without the performance waste by using default parameter:

Because sometimes you may want to edit data inside the array parameter and if it is passed by a reference or pointer then this could have unwanted effects. Like in my 'FuncAdd' where I've stored the result of the addition in first parameter, which if was a reference to array, would have also edited 'var' in the current example function instance. You can do this manually ofcourse, but the same apply for every other type, why should we ignore arrays?

Cool little snippet of code! It's nice to see some flexing of C++ muscles once in a while.

There are a few issues though.

First, you should also default the move constructor and move assignment operator. You could be wrapping an array of non-trivial types that are actually cheaper to move than to copy, you shouldn't pessimize your code. When you only explicitly default the copy functions, it implicitly deletes the compiler-generated move functions. So, you either have to leave them all to be implicitly defaulted or you need to explicitly default them all. So, either you have this:

Or you can have none of them. And because your type holds an array, you cannot write your own copy or move functions, they have to be default, explicit or not.

That's another issue with your code, on a practical level. Compilers currently have pretty unreliable behavior when it comes to this stuff (implicit and explicit defaulting rules of C++11). I just solved a critical bug in Boost related to this, which stemmed from the fact that the compilers GCC 4.5, GCC 4.6, GCC 4.9, MSVC (<12), and SGI MIPSpro, all have different, incorrect, and mutually incompatible behavior or support as far as those rules are concerned. The best way to get around it is to either provide user-defined functions for all of them, or leave them all to be implicitly defaulted. But, of course, that's more of a "real-world" issue, not so much an academic issue with the code.

Another issue with your code is that your parametrized constructor initializes the array in its body. If the intend is to have minimal overhead with your wrapper class, then you should avoid that. The reason for this is that because of things like exception handling, the compiler cannot really optimize away the default initialization of the array that is done before you actually initialize it in the body of the constructor. It might be able to do that optimization for trivial types, but I'm not even sure about that. To be safe, you should do the initialization in the initialization list, and to do that, you can use delegating constructors (another C++11 innovation):

Notice how the constructor simply calls the default copy-constructor with the casted array. You can also do this for a move constructor:

ugaw(type && arg) : ugaw(move(*reinterpret_cast<ugaw*>(&arg[0]))) { }

Also notice that I changed your C-style cast to a more explicit C++-style cast, because I didn't feel super at ease with your existing cast. There were just too many layers to this cast (array ref --> pointer to first element -- C-style cast --> ugaw pointer). Now, all those layers are explicit and easier to unravel.

And obviously, once you start with any move functions, you have to implement them everywhere, like this:

In the world of C++11, if you are going to talk about passing things by value, you should always implement move semantics, they go hand-in-hand.

And finally, you might want to consider additional constructors, especially one that takes a std::array (since people shouldn't use C-style static arrays anymore anyways) and one that takes a std::initializer_list. Either way, you will have to deduce the value-type of your array "type", which is good, because you can also use that as a compile-time check to make sure the user actually provides an array type and not something else (or do you want something else to be supported too?).

And at the end of the day, I really don't understand why you cannot use std::array directly instead? The standard array class is guaranteed to be an aggregate type, to have standard layout, and to be trivial (if the value-type allows for it), which is way more than you could ever say about your class. You say that the standard array class is going to be problematic on external linkage boundaries (I assume that you mean that there will be ABI compatibility issues if you use it), but that's not true. The standard array type is standard-layout, meaning that it is one of the few standard classes whose memory layout is guaranteed to be ABI compatible across any platform that comply with the same C ABI (which is fixed for a given target OS and CPU architecture). On that end of things, your class has nothing more to give than what the standard array class already does. Then, the fact that the standard array type is an aggregate type and possibly a trivial type can have substantial performance benefits that you cannot provide with your class the way it is. The only substantial differences here is that you provide implicit conversion to and from a C-style static array, which I am not convinced is a good idea to begin with, I mean, C-style static arrays are one of those things everyone would like to see disappear completely from day-to-day C++ code, in favor of the much superior std::array class (with all its zero-overhead STL-style member functions).

Edited
5 Years Ago
by mike_2000_17: edit

Be a part of the DaniWeb community

We're a friendly, industry-focused community of
1.18 million developers, IT pros, digital marketers,
and technology enthusiasts learning and sharing knowledge.