简体   繁体   English

什么接口会将cstrings,数组和其他类型复制到相同类型的实例?

[英]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.

相关问题 在同一解决方案中的项目之间传递CString - Passing CStrings between projects in same solution 如何公开C ++类型的静态成员实例,但不公开其他多余的接口/实现 - How to expose static member instances of a C++ type but no other superflous interface/implementation 对与其他阵列相同的数组进行排序 - Sorting arrays next to other arrays which are the same C ++什么类型的参数类型名称可以与带有类型说明符的参数名称相同? - C++ For what types can the parameter type name be the same as the parameter name with a type specifier? 将一种数据类型的向量复制到相同数据类型的结构向量中的有效方法是什么? - What is the efficient way to copy vectors of one data type into a vector of struct of the same data type? 哪些 C++ 对象(标准类型或 STYL)不能复制分配给另一个相同类型的对象? - What C++ objects (of standard type or STYL) can't be copy assigned to another of the same type? 在C ++中将迭代器反引用为映射或其他某些复合类型的类型是什么 - What is the type for dereferencing an iterator to a map or some other composite types in C++ 实例彼此独立的唯一类型的计数器 - Counter for unique types where instances are independent from each other 将字节从较小的类型复制到较大的类型 - Copy bytes from smaller types to bigger type 如何复制包含非原始类型的数组? - How do I copy arrays that contain non primitive types?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM