Jak przypisać alias do nazwy funkcji w C++?

Łatwo jest utworzyć nową nazwę dla typu, zmiennej lub przestrzeni nazw. Ale jak przypisać nową nazwę funkcji? Na przykład Chcę użyć nazwy holler dla printf. # define jest oczywiste... jest jakiś inny sposób?

Rozwiązania:

  1. #define holler printf
  2. void (*p)() = fn; //function pointer
  3. void (&r)() = fn; //function reference
  4. inline void g(){ f(); }
 84
Author: Agnel Kurian, 2010-06-16

6 answers

Istnieją różne podejścia:

  • Z C++11 z nie-szablonowymi nie przeciążonymi funkcjami można po prostu użyć:

    const auto& new_fn_name = old_fn_name;
    
  • Jeśli ta funkcja ma wiele przeciążeń należy użyć static_cast:

    const auto& new_fn_name = static_cast<OVERLOADED_FN_TYPE>(old_fn_name);
    

    Przykład: istnieją dwa przeciążenia funkcji std::stoi

    int stoi (const string&, size_t*, int);
    int stoi (const wstring&, size_t*, int);
    

    Jeśli chcesz utworzyć alias do pierwszej wersji, powinieneś użyć następującego:

    const auto& new_fn_name = static_cast<int(*)(const string&, size_t*, int)>(std::stoi);
    

    Uwaga: nie ma sposobu na przeciążenie aliasu funkcja taka, że wszystkie jego przeciążone wersje działają, więc zawsze należy określić, które dokładne przeciążenie funkcji chcesz.

  • Z C++14 możesz pójść jeszcze dalej ze zmiennymi szablonów constexpr. Pozwala to na aliasy funkcji szablonowych:

    template<typename T>
    constexpr void old_function(/* args */);
    
    template<typename T>
    constexpr auto alias_to_old = old_function<T>;
    
  • Ponadto, począwszy od C++11 masz funkcję o nazwie std::mem_fn, która pozwala na aliasy funkcji Członkowskich. Zobacz następujący przykład:

    struct A {
       void f(int i) {
          std::cout << "Argument: " << i << '\n';
       }
    };
    
    
    A a;
    
    auto greet = std::mem_fn(&A::f); // alias to member function
    // prints "Argument: 5"
    greet(a, 5); // you should provide an object each time you use this alias
    
    // if you want to bind an object permanently use `std::bind`
    greet_a = std::bind(greet, a, std::placeholders::_1);
    greet_a(3); // equivalent to greet(a, 3) => a.f(3);
    
 91
Author: sasha.sochka,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-01-13 19:01:12

Możesz utworzyć wskaźnik funkcji lub odniesienie do funkcji:

void fn()
{
}

//...

void (*p)() = fn;//function pointer
void (&r)() = fn;//function reference
 32
Author: Brian R. Bondy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-06-16 13:22:12
typedef int (*printf_alias)(const char*, ...);
printf_alias holler = std::printf;
Powinno Ci wystarczyć.
 21
Author: jer,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-06-16 13:21:31

int (*holler)(const char*, ...) = std::printf;

 10
Author: MSalters,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-06-16 13:20:56

Użyj wrappera. Otrzymujesz oba interfejsy API, ale zachowasz jedną implementację.

 7
Author: John,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-06-16 19:07:11

From fluentcpp : ALIAS_TEMPLATE_FUNCTION (f, g)

#define ALIAS_TEMPLATE_FUNCTION(highLevelF, lowLevelF) \
template<typename... Args> \
inline auto highLevelF(Args&&... args) -> decltype(lowLevelF(std::forward<Args>(args)...)) \
{ \
    return lowLevelF(std::forward<Args>(args)...); \
}
 1
Author: sailfish009,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-01-04 02:20:09