Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;通过引用传递已分配的内存/数组_C++ - Fatal编程技术网

C++ C++;通过引用传递已分配的内存/数组

C++ C++;通过引用传递已分配的内存/数组,c++,C++,已解决 我正在为一个处理结构bwords的现有lib编写一个接口(见下面的代码),并希望提供对bword本身或字节字符串(bword成员)调用一些检查函数的可能性: #包括 typedef无符号字符字节; typedef无符号短ushort; typedef struct bwordSt{ushort nbLetters;byte*L;}bwords; 模板 ushort checkBwL(T&wL)[N],ushort wSz){ 返回0; } ushort checkBwL(常量字节*常量和

已解决

我正在为一个处理结构bwords的现有lib编写一个接口(见下面的代码),并希望提供对bword本身或字节字符串(bword成员)调用一些检查函数的可能性:

#包括
typedef无符号字符字节;
typedef无符号短ushort;
typedef struct bwordSt{ushort nbLetters;byte*L;}bwords;
模板
ushort checkBwL(T&wL)[N],ushort wSz){
返回0;
}
ushort checkBwL(常量字节*常量和wL,ushort wSz){
返回0;
}
ushort checkBw(const bword&bw){
返回checkBwL(bw.L,bw.nbLetters);
}
int main(){
乌肖特n;
字节fL[2]={0,1};
n=checkBwL(fL,2);//调用模板函数
bword bW={2,新字节[3]};
bW.L[0]=0;bW.L[1]=1;bW.L[2]=2;
n=checkBwL(bW.L,3);//调用非模板函数
n=checkBw(bW);//调用非模板函数
返回n;
}
字节字符串可能很大,所以我想通过引用传递。我做到了

我发现提供统一接口的唯一方法是在模板中复制基本检查函数(checkBwL)的代码(对于数组[byte])和重载(对于byte*),这很难看,迫使我维护两个基本相同的(大)函数

有办法吗

解决方案


不需要模板函数,只是不要忘记参数规范中
常量字节*const&wL

&
前面的
常量
成功的关键是委派:

#include <cstdio> 

typedef unsigned char byte;
typedef unsigned short ushort;
typedef struct bwordSt { ushort nbLetters;  byte *L; } bword;

ushort check_impl(ushort length, const byte* buffer)
{
    // do your actual checking here
    return 0;
}

template<typename T, size_t N>
auto checkBw(T (&wL)[N], ushort wSz) -> ushort
{
    return wSz == (N * sizeof(T)) &&  // assuming no null terminator 
    check_impl(wSz, reinterpret_cast<const byte*>(wL));
}

ushort checkBw(const byte* const &wL, ushort wSz) {
  return check_impl(wSz, wL);
}

ushort checkBw(const bword &bw) {   
  return check_impl(bw.nbLetters, bw.L);  
}

int main() {
  ushort n;
  byte fL[2] = {0, 1};
  n = checkBw(fL, 2);  // calls the template function

  bword bW = {2, new byte[3]};
  bW.L[0] = 0; bW.L[1] = 1; bW.L[2] = 2; 
  n = checkBw(bW.L, 3);  // calls the non-template function
  n = checkBw(bW);        // calls the non-template function

  return n;
}
#包括
typedef无符号字符字节;
typedef无符号短ushort;
typedef struct bwordSt{ushort nbLetters;byte*L;}bwords;
ushort check_impl(ushort长度,常量字节*缓冲区)
{
//在这里进行实际检查
返回0;
}
模板
自动检查BW(T(&wL)[N],ushort wSz)->ushort
{
返回wSz==(N*sizeof(T))&&&//假设没有空终止符
检查安装(wSz,重新解释铸造(wL));
}
ushort checkBw(常量字节*常量和wL,ushort wSz){
退货检查单(wSz、wL);
}
ushort checkBw(const bword&bw){
返回检查(bw.nbLetters,bw.L);
}
int main(){
乌肖特n;
字节fL[2]={0,1};
n=checkBw(fL,2);//调用模板函数
bword bW={2,新字节[3]};
bW.L[0]=0;bW.L[1]=1;bW.L[2]=2;
n=checkBw(bW.L,3);//调用非模板函数
n=checkBw(bW);//调用非模板函数
返回n;
}

checkBWL做什么?@NathanOliver:修复@理查德:很多东西,结构实际上比这丰富得多。你为什么不直接从模板函数调用非模板函数呢?看起来你希望
checkBw()
应该调用
checkBwL()的模板版本。但它不会。对于模板化版本,您希望使用什么样的用例?我正在试图弄清楚为什么您需要模板函数,而我不能。它基本上与您的非模板函数相同,非模板版本应该使用指针或数组。实际上,正如上面NathanOliver所述,不需要模板函数。不过,您建议的技术非常有趣,尽管我的C++11编译器不喜欢这样:test.cpp:14:36:error:“checkBw”函数使用“auto”类型说明符,但没有尾随返回类型:auto checkBw(…无论如何,谢谢,这是需要记住的。@ExpertNoob1啊,好的,我已经在那里使用了c++14功能。我很抱歉。@ExpertNoob1…和编辑使其与c++11兼容。
#include <cstdio> 

typedef unsigned char byte;
typedef unsigned short ushort;
typedef struct bwordSt { ushort nbLetters;  byte *L; } bword;

ushort check_impl(ushort length, const byte* buffer)
{
    // do your actual checking here
    return 0;
}

template<typename T, size_t N>
auto checkBw(T (&wL)[N], ushort wSz) -> ushort
{
    return wSz == (N * sizeof(T)) &&  // assuming no null terminator 
    check_impl(wSz, reinterpret_cast<const byte*>(wL));
}

ushort checkBw(const byte* const &wL, ushort wSz) {
  return check_impl(wSz, wL);
}

ushort checkBw(const bword &bw) {   
  return check_impl(bw.nbLetters, bw.L);  
}

int main() {
  ushort n;
  byte fL[2] = {0, 1};
  n = checkBw(fL, 2);  // calls the template function

  bword bW = {2, new byte[3]};
  bW.L[0] = 0; bW.L[1] = 1; bW.L[2] = 2; 
  n = checkBw(bW.L, 3);  // calls the non-template function
  n = checkBw(bW);        // calls the non-template function

  return n;
}