在C ++ 11中使用数组init列表作为临时数组? [英] Using array init list as temporary in C++11?

查看:165
本文介绍了在C ++ 11中使用数组init列表作为临时数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以用如下的数组创建一个命名变量:

I can create a named variable with an array as follows:

char s[] = {1, 2, 3, 0};
if (strcmp(s, t))
    ...

以下不工作:

if (strcmp(char[]{1,2,3,0}, t))
    ...

有一种方法可以使用初始化程序指定临时未命名数组列表? (在这种情况下,字符串字面量将工作,但对于数组而不是char数组?)

Is there some way to specify a temporary unnamed array with an initializer list? (In this case a string literal would work, but for arrays other than char arrays?)

更新: b

#include <iostream>
#include <cstring>

using namespace std;

typedef char CA[];

int main()
{
        cout << CA{1,2,3, 0} << endl;
}

给出错误: code>( g ++ - 4.7.2 -std = gnu ++ 11

更新2:

我想(也许)发生的是字符串文字特别祝福为 lvalues prvalues ,因此您无法收到他们的地址。这是一个疯狂的猜测,但我不确定。

I think (maybe) what is happening is that string literals are specially blessed as lvalues, however temporary arrays are prvalues, and as such you cant take their address. This is a wild guess though, I'm not sure.

更新3:

实际上应该是错的我认为:

Actually that should be wrong I think:


类型为array of NT或array of unknown bound的左值或右值of T可以转换为类型指针到T的prvalue
。结果是指向数组第一个元素的指针。

An lvalue or rvalue of type "array of N T" or "array of unknown bound of T" can be converted to a prvalue of type "pointer to T". The result is a pointer to the first element of the array.


推荐答案

有这个问题一段时间。编译以下程序,g ++ 4.7.1(tdm64-1)出现错误:teste1.cpp:6:33:error:take address of temporary array

1) I suffered with this problem for some time. Compiling the following program, g++ 4.7.1 (tdm64-1) gives the error: "teste1.cpp:6:33: error: taking address of temporary array"

#include <iostream>
#include <cstring>
using namespace std;
int main()
{ char t[]={1,2,3,0};
  if (strcmp((char[]){1,2,3,0},t)==0)  //error
    cout << "equal\n";
  else 
    cout << "different\n";
}

但是,如果添加关键字const,错误消失,程序运行平稳:

However, if you add the keyword "const", the error disappears and the program runs smoothly:

if (strcmp((const char[]){1,2,3,0},t)==0) //correct

2)在某些情况下,只要添加关键字const不够。例如,当编译以下程序时,g ++ 4.7.1给出错误:获取临时数组的地址:

2) In some cases, just adding the keyword "const" may not be enough. For example, g++ 4.7.1 gives "error: taking address of temporary array" when compiling the following program:

#include <iostream>
#include <cstring>
using namespace std;

void f(char* p)
{ for (int i=0; p[i]!=0; i++) 
    cout << int(p[i]) << " ";
  cout << endl;
}

int main() {
  f((char[]){1,2,3,0}); // error
}



如果添加关键字const种类的错误:从'const char *'到'char *'无效转换[-fpermissive]:

If you add the keyword "const", the compiler gives another kind of error: "invalid conversion from 'const char*' to 'char*' [-fpermissive]":

f((const char[]){1,2,3,0}); //error

要成功编译程序,可以使用选项-fpermissive make a explicit type conversion:

To successfully compile the program, you can either compile it with option "-fpermissive" or make a explicit type conversion:

f((char*)(const char[]){1,2,3,0}); // correct

这篇关于在C ++ 11中使用数组init列表作为临时数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆