[英]What interface will copy cstrings, arrays, and other types to instances of the same type?
Not all instances of a type can be copied to another instance of the same type with an =
sign. 并非所有类型的实例都可以使用=
符号复制到同一类型的另一个实例。
For example, although it may work on ints : 例如,虽然它可能适用于整数 :
int x = 0;
int y = 5;
x = y; //x is now: 5
It would not work on char arrays : 它不适用于char数组 :
char x[32]="data to overwrite";
char y[32]="new data";
x = y; //incorrect
or other arrays : 或其他数组 :
int x[5] = {1,2,3,4,5};
int y[5] = {6,7,8,9,10};
x = y; //incorrect
or char*s : 或者char * s :
char* x="data to overwrite";
char* y="new data";
x = y; //incorrect
How can I write an overloaded function that will allow me to do the following? 如何编写一个允许我执行以下操作的重载函数?
int x = 0;
int y = 5;
Copy(x,y); //x is now: 5
char x[32]="data to overwrite";
char y[32]="new data";
Copy(x,y); //x is now: "new data"
int x[5] = {1,2,3,4,5};
int y[5] = {6,7,8,9,10};
Copy(x,y); //x is now: {6,7,8,9,10}
char* x="data to overwrite";
char* y="new data";
Copy(x,y); //x is now: "new data"
*I'll assume that any abstract data types do the necessary work in their overloaded assignment operator(or from the shallow copy provided by the compiler) *我将假设任何抽象数据类型在其重载赋值运算符(或从编译器提供的浅拷贝)中执行必要的工作
Why do you need to do this? 你为什么需要这样做?
In order to easier test portions of a legacy C code base, I'd like to generate some C++ wrappers around a few components. 为了更容易地测试遗留C代码库的部分,我想围绕几个组件生成一些C ++包装器。 Due to the strange design of the C code, there is a lot of indirection that I would like to get rid of. 由于C代码的奇怪设计,我想摆脱很多间接。 As such, it would be a lot easier to copy variables into another instance using a simple Copy
function instead of parsing out the types, and deciding how to make the appropriate copy into the other instance variable. 因此,使用简单的Copy
函数将变量复制到另一个实例而不是解析类型,并决定如何将适当的副本复制到另一个实例变量中要容易得多。
Here is a complete example, with cout
s sprinkled in to show that the correct overloads are selected. 这是一个完整的例子,其中有cout
s表示选择了正确的重载。
#include <algorithm>
#include <cstddef>
#include <iostream>
#include <ostream>
// default
template<class T>
void Assign(T& dst, const T& src)
{
dst = src;
std::cout << "assign (default)" << std::endl;
}
// arrays
template<class T1, std::size_t n>
void Assign(T1 (&dst)[n], const T1 (&src)[n])
{
std::copy(src, src+n, dst);
std::cout << "assign (array)" << std::endl;
}
// pointers
template<class T1>
void Assign(T1 *&dst, T1 *src)
{
// DANGER: memory leaks/double frees
// not exactly sure what is supposed to happen here
// same as default for now...
// ok as long as only string constants are passed around
// (as is the case in the example)
dst = src;
std::cout << "assign (pointer)" << std::endl;
}
int main() {
{
int x = 0;
int y = 5;
Assign(x,y); //x is now: 5
}
{
char x[32]="data to overwrite";
char y[32]="new data";
Assign(x,y); //x is now: "new data"
}
{
int x[5] = {1,2,3,4,5};
int y[5] = {6,7,8,9,10};
Assign(x,y); //x is now: {6,7,8,9,10}
}
{
const char* x="data to overwrite";
const char* y="new data";
Assign(x,y); //x is now: "new data"
}
}
Output: 输出:
g++ -std=c++11 -g -Wall -O3 check.cc -o check && ./check
assign (default)
assign (array)
assign (array)
assign (pointer)
Here is my attempt on my own question: 以下是我对自己问题的尝试:
#include <algorithm>
#include <cstring>
//catch all
template <typename T>
void Copy(T &x, T y)
{
x = y;
}
//overload on static array copying
//(T[N] = T[N])
template <typename T, size_t N>
void Copy(T(&x)[N], T(&y)[N])
{
std::copy(std::begin(y), std::end(y), std::begin(x));
}
//overload on copying of null terminated data
//(char array = cstring)
void Copy(char x[], const char y[])
{
//assumes x is >= to y
//not sure if I can use strncpy somewhow
strcpy(x, y);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.