转换匿名数组初始化器列表 [英] Casting an anonymous array initializer list
问题描述
我可以成功地对char字符串数组的初始化列表进行C转换,但似乎无法使其与C ++转换(static_cast)一起使用:
I can successfully do a C cast of an initializer list for an array of char strings, but can't seem to get it to work with a C++ cast (static_cast):
int main()
{
char x[] = "test 123";
// This works fine:
char **foo = (char *[]) { "a", x, "abc" };
std::cout << "[0]: " << foo[0] << " [1]: " << foo[1]
<< " [2]: " << foo[2] << std::endl;
// This will not compile ("expected primary-expression before '{' token"):
//char **bar = static_cast<char *[]>( { "a", x, "abc" } );
//std::cout << "[0]: " << bar[0] << " [1]: " << bar[1]
// << " [2]: " << bar[2] << std::endl;
}
是否可以在此处使用C ++强制转换?如果是这样,正确的语法是什么?如果不是,为什么不这样做,C转换是否让我摆脱了我不应该做的事情?
Is it possible to use a C++ cast here? If so, what's the correct syntax? If not, why not, and is the C cast letting me get away with something I shouldn't be doing?
最终,我问这个的原因是我正在调用将char数组指针作为参数的函数,并且我想使用匿名数组作为调用参数。
Ultimately, the reason I'm asking this is that I am calling a function that has a char array pointer as a parameter, and I would like to use an anonymous array as the calling argument.
我正在使用GCC 4.4.6。
I'm using GCC 4.4.6.
推荐答案
我可以成功地对一个char字符串数组的初始化列表进行C转换
I can successfully do a C cast of an initializer list for an array of char strings
不,可以没错您根本没有使用初始化列表或C强制转换。您使用的是复合文字。这是C语言所不具备的C语言功能。某些编译器确实支持C ++作为语言扩展。
No, you can't. You didn't use an initializer list nor a C cast at all. What you used was a compound literal. It is a C language feature that doesn't exist in C++. Some compilers do support them in C++ as a language extension.
我强烈建议您使用编译器选项,该选项至少在使用非标准功能时会发出警告,以避免混淆
I highly recommend you to use a compiler option that at least warns when you use non-standard features to avoid confusion like this.
,但似乎无法使其与C ++ cast一起使用
but can't seem to get it to work with a C++ cast
您不能强制转换初始化列表表达式。您将通常必须先初始化一个命名数组,然后再初始化该指针-尽管您几乎不需要真正使用单独的指针变量,因为无论如何该数组在大多数情况下都隐式地衰减为指针。
You cannot cast an initializer list expression. You will have to initialize a named array normally, then the pointer - although you hardly ever really need a separate pointer variable since the array implicitly decays to a pointer in most contexts anyway.
const char* arr[] = { "a", x, "abc" };
const char** foo = arr;
我的原因我问这是我正在调用一个以char数组指针作为参数的函数,并且我想使用匿名数组作为调用参数。
the reason I'm asking this is that I am calling a function that has a char array pointer as a parameter, and I would like to use an anonymous array as the calling argument.
如果可以修改该函数,则可以使用多种方法来允许调用而无需命名数组。您可以接受 std :: initializer_list
,也可以接受可以从初始化列表构造的类型,例如 std :: array
。
If you can modify the function, then there are ways to allow the call without a named array. You could accept an std::initializer_list
, or a type that can be constructed from an initializer list such as an instance of std::array
.
PS。从字符串文字到 char *
的隐式转换在C ++中也是不允许的-但某些编译器允许作为语言扩展。在此处使用 const char *
。
PS. Implicit conversion from string literal to char*
is not allowed in C++ either - but allowed by some compilers as a language extension. Use const char*
here.
这篇关于转换匿名数组初始化器列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!