English 中文(简体)
我如何在C++中指定一个职务名称的别名?
原标题:How do I assign an alias to a function name in C++?
  • 时间:2010-06-16 13:19:09
  •  标签:
  • c++
  • alias

很容易为某种类型、变数或名称空间创造新的名称。 但是,我如何为一项职能分配新的名称? 例如,我要使用<代码>holler>>>>>,。 事实是显而易见的。

<>Solutions:

  1. #define holler printf
  2. void (*p)() = fn; //function pointer
  3. void (&r)() = fn; //function reference
  4. inline void g(){ f(); }
最佳回答

不同做法:

  • 由于C++11具有非固定的未上载职能,你只能使用:

    const auto& new_fn_name = old_fn_name;
    
  • 如果该功能多载,请使用static_cast:

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

    例:

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

    如果你想对第一版做一个部分的话,你应当使用以下文字:

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

    说明: 不能用其他方法超负荷运作,这样,所有超负荷版本的工作就一定能够具体确定哪些功能超出你想要的负荷。

  • 有了C++14,你可以进一步使用<代码>constexpr模板变量。 这使你能够把模版职能具体化:

    template<typename T>
    constexpr void old_function(/* args */);
    
    template<typename T>
    constexpr auto alias_to_old = old_function<T>;
    
  • 此外,从C++11开始,你有以下职能:m_fn,允许将成员的职能包括在内。 见以下例子:

    struct A {
       void f(int i) {
          std::cout << "Argument: " << i <<  
     ;
       }
    };
    
    
    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);
    
问题回答

您可设立职能协调人或职能参考资料:

void fn()
{
}

//...

void (*p)() = fn;//function pointer
void (&r)() = fn;//function reference
typedef int (*printf_alias)(const char*, ...);
printf_alias holler = std::printf;

如果是罚款的话。

www.un.org/Depts/DGACM/index_russian.htm ...... = st;

使用网上包装。 你们都得到二者,但保持单一执行。

有了C++14通用斜线,我就能够做以下工作,如果目标功能多载:

constexpr auto holler = [] ( auto &&...args ) {
        return printf( std::forward<decltype(args)>( args )... );
    };

这里值得一提的是,虽然最初的问题(和很大的答案)如果你想要重新命名一项职能(这是很好的理由!)无疑是有用的,但如果你想要这样做的话,那么你就会把一个深名的空间 strip掉,而保留名称,那么就存在“<>>>><>>>>>>><>条码”。 关键词:

namespace deep {
  namespace naming {
    namespace convention {
      void myFunction(int a, char b) {}
    }
  }
}
int main(void){
  // A pain to write it all out every time
  deep::naming::convention::myFunction(5,  c );

  // Using keyword can be done this way
  using deep::naming::convention::myFunction;
  myFunction(5,  c );  // Same as above
}

这还具有局限性的优势,尽管你总是可以在档案的最高层使用。 我经常在<条码>、<条码>和<条码>>上使用,因此,我无需在<条码>中填入经典的<条码>;在案卷顶上填入;;但如果你重新使用像<条码”这样的东西,也是有益的:这是指:在案卷或功能上,但并非在任何地方,而不是从空间上传出任何其他功能。 一如既往,它劝阻在案卷中加以使用,或 you污全球名称空间。

这与上文提到的“欢迎”并不相同,但往往是实际想要的。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签