In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
How to use the using keyword in C++. For this question, this article introduces the corresponding analysis and answer in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.
C.165: use the using keyword for customization points
Reason (reason)
To find function objects and functions defined in a separate namespace to "customize" a common function.
To find function objects and functions that are defined in a separate namespace in order to customize common functions.
Example (sample)
Consider swap. It is a general (standard-library) function with a definition that will work for just about any type. However, it is desirable to define specific swap () s for specific types. For example, the general swap () will copy the elements of two vectors being swapped, whereas a good specific implementation will not copy elements at all.
Consider the exchange function. It is a general (standard library) that can be applied to any type of function. However, you also want to be able to define special exchange functions for special types. For example, a normal swap function copies the elements of the vector as the swap object, while a good special implementation should not copy the elements at all.
Namespace N {
My_type X {/ *... * /}
Void swap (X-ray, X &); / / optimized swap for N-V-V-V
/ /...
}
Void F1 (NRV V V X & a, N V X & b)
{
Std::swap (a, b); / / probably not what we wanted: calls std::swap ()
}
The std::swap () in F1 () does exactly what we asked it to do: it calls the swap () in namespace std. Unfortunately, that's probably not what we wanted. How do we get N::X considered?
Std::swap () in function F1 does exactly what we want: it calls swap () in the std namespace. Unfortunately, that may not be what we want. How can we implement the NRV X we look forward to?
Void f2 (NRV V V X & a, N V X & b)
{
Swap (a, b); / / calls N::swap
}
But that may not be what we wanted for generic code. There, we typically want the specific function if it exists and the general function if not. This is done by including the general function in the lookup for the function:
But this (as in the above code, translator's note) is not what normal code should look like. Our general idea here is that if there is a special function, execute it instead of the general function. The way to achieve this function is to include the general function within the retrieval scope of the function.
Void f3 (NRV V V X & a, N V X & b)
{
Using std::swap; / / make std::swap available
Swap (a, b); / / calls N::swap if it exists, otherwise std::swap
}
So much for the answers to the questions on how to use the using keyword in C++. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.