C ++ multiple casting rule

In the following code

    std::transform (source.begin(), source.end(),  // start and end of source 
            dest.begin(),                  // start of destination 
            (int(*)(int const&)) addValue<int,5>);  // operation 

Can someone break the throw

    (int(*)(int const&))

where addValue is a Nontype function template, like

    template <typename T, int VAL> 
    T addValue (T const& x) 
    { 
        return x + VAL; 
    } 

Thank.

+4
source share
1 answer

Listing (int(*)(int const&))is a cast to a type int(*)(int const&)that is a "pointer to a function that accepts int const&and returns int" type.

Since it is addValue<int, 5>already of the type "function receiving int const&and returning int" (and will decay to a function pointer when passing by value), in this context, casting is not required.

, , . addValue :

template <typename T, int VAL>
void addValue(T& x) {
    x += VAL;
}

addValue<int, 5> . , addValue , int addValue<int, 5>(int const&) void addValue<int, 5>(int&), , .

+4

Source: https://habr.com/ru/post/1658575/


All Articles