Are there real cases proving that "size ..." is necessary?

I wonder about the benefits of the new operator sizeof...

(not to be confused with the operator sizeof

). I searched the internet and found several examples that seem similar to the following:

template<class... ArgTypes>
std::size_t GetLength()
{
    return sizeof...(ArgTypes);
}

      

I think the examples are not illustrative.

Are there real-world examples to illustrate which is sizeof...

very helpful?

Update:

I found some more examples from here that seem more meaningful:

template<class ...A> void func(A ...args){
   typedef typename common_type<A...>::type common;
   std::array<common, sizeof...(A)> a = {{ args... }};
}

template<typename... A> int func(const A&... args)
{
  boost::any arr[sizeof...(A)] = { args... };
  return 0;
}

      

+3


source to share


3 answers


Here's my example of what you can do with sizeof...

:

/// Transform a single boolean value into a number
constexpr unsigned int boolCode(bool value) {
    return value;
}

/// Transform a sequence of booleans into a number
template <typename... Args>
constexpr unsigned int boolCode(bool value, Args... others) {
    return value << sizeof...(others) | boolCode(others...);
}

      



And this handy function can be used in a switch statement, for example:

switch (boolCode(condition1, condition2, condition3)) {
case boolCode(false,false,false): //...
case boolCode(false,false,true): //...
case boolCode(false,true,false): //...
case boolCode(false,true,true): //...
case boolCode(true,false,false): //...
case boolCode(true,false,true): //...
case boolCode(true,true,false): //...
case boolCode(true,true,true): //...
}

      

+3


source


you probably want to read the discussion between STL and CornedBee in the comments: http://channel9.msdn.com/Series/C9-Lectures-Stephan-T-Lavavej-Core-C-/Stephan-T-Lavavej-Core-Cpp -8-of-n # comments

Important bit:



sizeof ... isn't just syntactic sugar. A manual implementation of sizeof ... will have a linear "execution time" (number of instances), while the built-in sizeof ... is O (1). (One big question about variations as they are is that compilation tends to be very slow due to the lack of random access to the argument packages. Some guy (from Boost, I think) looked into this and found that the compilation speed of Boost. Tuple (a preprocessor nonvariant tuple) compiles significantly faster than the naive variation-based version.)

+2


source


The first and foremost reason sizeof

was introduced in C ++ because it was present in C where it is needed in order to know how much memory to allocate, for example. malloc( n * sizeof(struct MyClass) )

... In C ++, it has been used in similar cases where allocation and initialization are separate, for example in container classes or variants, or perhaps in classes.

It is also known to be used in template meta-programming, in conjunction with the permission override function. Things along the lines: sizeof( discriminatorFunction( someArgs ) ) == sizeof( TrueType )

.

-3


source







All Articles