很容易为某种类型、变数或名称空间创造新的名称。 但是,我如何为一项职能分配新的名称? 例如,我要使用<代码>holler>>>>>,
<>Solutions:
#define holler printf
void (*p)() = fn; //function pointer
void (&r)() = fn; //function reference
inline void g(){ f(); }
很容易为某种类型、变数或名称空间创造新的名称。 但是,我如何为一项职能分配新的名称? 例如,我要使用<代码>holler>>>>>,
<>Solutions:
#define holler printf
void (*p)() = fn; //function pointer
void (&r)() = fn; //function reference
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 )... );
};
http://www.fluentcpp.com/2017/10/27/Function-aliases-cpp/“rel=“noreferer”>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)...);
}
这里值得一提的是,虽然最初的问题(和很大的答案)如果你想要重新命名一项职能(这是很好的理由!)无疑是有用的,但如果你想要这样做的话,那么你就会把一个深名的空间 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污全球名称空间。
这与上文提到的“欢迎”并不相同,但往往是实际想要的。
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 ...
I have been searching for sample code creating iterator for my own container, but I haven t really found a good example. I know this been asked before (Creating my own Iterators) but didn t see any ...
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 ...
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?
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->...
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, ...
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 ...
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?