Type of decomposition - what is it and why is it there?

I was surprised that type decay is not well explained on SO or elsewhere, maybe I did not look for the correct terms, or maybe I do not understand everything correctly. My question is: what is it, how (why) did it get there, and what are its rules?


If you are wondering why I ask, below is a story about the decay of my sob ( not the topic of the question):

I recently struggled with some simple patterns, and I wanted to do something like this:

template <class FunObj>
double DoStuff(FunObj fob) // note that this returns double, not FunObj, as e.g. std::for_each() does
{ /* ... */ }

struct MyFunObj {
    mutable size_t num_invoked; // collect usage statistics

    MyFunObj()
        :num_invoked(0)
    {}

    bool operator ()(double some_arg) const
    {
        ++ num_invoked;
        return sin(some_arg) < .5;
    }
};

, , , . , ( , ), . (, , ):

MyFunObj fob;
DoStuff(fob); // passed by value, usage statistics are lost (no surprise here)

MyFunObj &ref = fob;
DoStuff(ref); // MyFunObj& decays to MyFunObj, usage statistics are lost

DoStuff(static_cast<MyFunObj&>(fob)); // again, hampered by type decay

, , , , . , / . , mutable.

+4
1

, . "" " ", " " "lvalue-to-rvalue". , , - .

, , . ref - MyFunObj, lvalue (BTW, fob). , FunObj ref, MyFunObj.

++ 11 . ++ 11 " ". , U , T&& ( T ) U& U. , . .

+7

All Articles