我对这两个符号表示什么感到非常困惑。我知道 的优先级()大于[],这是否意味着它char(*name)[10]是一个指针并且char *name[10]是一个数组?
以下不会编译:
#include <memory>
class A;
bool f() {
std::shared_ptr<A> a;
return a;
}
int main()
{
f();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
并失败:
Compilation failed due to following error(s).main.cpp: In function ‘bool f()’:
main.cpp:13:12: error: cannot convert ‘std::shared_ptr’ to ‘bool’ in return
return a;
Run Code Online (Sandbox Code Playgroud)
标准(我认为)不允许在这里进行隐式转换的原因是什么?
长话短说,我正在制作这个 3D OpenGL 构建器游戏。细节并不重要,但这是我的问题。我有这个字符串数组:
char building_category_names_UPPER_CASE[][30] = { "NOTHING", "WAREHOUSE", "PROCESSING PLANT", "FACTORY", "OFFICE" };
Run Code Online (Sandbox Code Playgroud)
我试图将它们传递给一个函数,该函数接受字符串数组形式的“列表”,并通过 Create_Button 方法为该列表中的每个项目创建按钮。
void Create_Button_List(char** list, int list_size, char* group_name, int pos_x, int pos_y)
{
for (int i = 1; i < list_size; i++)
{
char name[50];
strcpy(name, list[i]);
char group[50];
strcpy(group, group_name);
Create_Button(name, group, pos_x, i * pos_y, 205, 50, text_color_white, bg_color, text_color_white, bg_color_hover);
}
}
Run Code Online (Sandbox Code Playgroud)
但即使 VS 在构建时没有给我任何错误,当它到达 strcpy (name, list[i]); 时我仍然收到访问冲突错误。部分。
这里有什么问题?这不是我应该如何传递二维字符数组吗?或者是我将其传递给 strcpy 的方式有问题?需要明确的是,在本例中 list_size 为 5,所以这是正确的,问题不应该是 for 循环越界。
如果有人可以帮助我,请提前致谢!
c pointers multidimensional-array implicit-conversion function-declaration
我有这个类SmallInt应该表示范围内的正整数值0-255- 包括:
struct SmallInt{
explicit SmallInt(int x = 0) : iVal_( !(x < 0 || x > 255) ? x :
throw std::runtime_error(std::to_string(x) + ": value outbounds!")){}
operator int&() { return iVal_; }
int iVal_;
};
int main(){
try{
SmallInt smi(7);
cout << smi << '\n';
cout << smi + 5 << '\n'; // 7 + 5 = 12
cout << smi + 5.88 << '\n'; // 7.0 + 5.88 = 12.88
smi = 33; // …Run Code Online (Sandbox Code Playgroud) c++ explicit-constructor conversion-operator implicit-conversion
std::string_view下面这段从到转换的代码怎么可能编译std::string:
struct S {
std::string str;
S(std::string_view str_view) : str{ str_view } { }
};
Run Code Online (Sandbox Code Playgroud)
但是这个不能编译?
void foo(std::string) { }
int main() {
std::string_view str_view{ "text" };
foo(str_view);
}
Run Code Online (Sandbox Code Playgroud)
第二个给出错误:cannot convert argument 1 from std::string_view to std::string和no sutiable user-defined conversion from std::string_view to std::string exists。
应该如何foo()正确打电话呢?
std::vector\xe2\x80\x98s 初始值设定项列表构造函数具有以下形式
vector( std::initializer_list<T> init, const Allocator& alloc = Allocator() );\nRun Code Online (Sandbox Code Playgroud)\n是什么使得这样的初始化std::vector<string> vec{ \xe2\x80\x9cfoo\xe2\x80\x9d, \xe2\x80\x9cbar\xe2\x80\x9d };成为可能?为什么构造函数接受一个std::initializer_list<const char*>,即使std::vectors\xe2\x80\x98sT是std::string?std::initializer_list<const char*>从到 的转换在哪里以及如何进行std::initializer_list<string>发生?
c++ implicit-conversion c++11 stdinitializerlist braced-init-list
使用 int 进行 float 隐式转换时,失败并显示 printf()
#include <stdio.h>
int main(int argc, char **argv) {
float s = 10.0;
printf("%f %f %f %f\n", s, 0, s, 0);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
用它编译时gcc -g scale.c -o scale会输出垃圾
./scale
10.000000 10.000000 10.000000 -5486124068793688683255936251187209270074392635932332070112001988456197381759672947165175699536362793613284725337872111744958183862744647903224103718245670299614498700710006264535590197791934024641512541262359795191593953928908168990292758500391456212260452596575509589842140073806143686060649302051520512.000000
Run Code Online (Sandbox Code Playgroud)
如果我显式地将整数转换为浮点数,或者使用 a 0.0(这是 a double),它会按设计工作。
#include <stdio.h>
int main(int argc, char **argv) {
float s = 10.0;
printf("%f %f %f %f\n", s, 0.0, s, 0.0);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当用它编译时gcc -g scale.c -o scale确实输出预期的输出
./scale
10.000000 …Run Code Online (Sandbox Code Playgroud) c printf type-conversion implicit-conversion conversion-specifier
我想知道为什么这段代码的两个版本都没有错误:
#include <iostream>
bool check_number(int n) {
return n < 5;
}
int main() {
int number = 6;
// (1) prints "Hello World"
if (check_number)
// (2) prints "not hello world"
if (check_number(number))
{
std::cout << "Hello world!";
}
else
{
std::cout << "not hello world";
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Hello world!尽管我没有将变量传递到if 语句中的number函数中,但条件 (1) 仍会打印。check_number
在情况(2)中,我得到了not hello world预期的结果。
template<typename T>
void func(const T &x) {
cout << "base template "<<x<<"\n";
}
template<>
void func(const int &x) {
cout << "int specialization "<<x<<"\n";
}
template<>
void func(const double &x) {
cout << "double specialization "<<x<<"\n";
}
int main() {
int five = 5;
func(five); // prints int specialization 5
double d = 3.14;
func<int>(d); // prints int specialization 3
}
Run Code Online (Sandbox Code Playgroud)
现在已const删除
template<typename T>
void func( T &x) {
cout << "base template "<<x<<"\n";
}
template<>
void func( …Run Code Online (Sandbox Code Playgroud) 这个问题建立在这个问题的基础上,它描述了以下内容如何等效:
int f(int a[10]) { ... } // the 10 makes no difference
int f(int a[]) { ... }
int f(int *a) { ... }
Run Code Online (Sandbox Code Playgroud)
在有关函数原型作用域的文档中,提供了以下示例:
int f(int n, int a[n]); // n is in scope, refers to first param
Run Code Online (Sandbox Code Playgroud)
这让我质疑以下内容在多大程度上是等效的:
// 1.
int f(int n, int a[n]) { ... }
int f(int n, int *a) { ... }
// my guess: exactly equivalent
// 2.
int x = 10; int f(int a[x]) { ... } …Run Code Online (Sandbox Code Playgroud) c arrays language-lawyer implicit-conversion function-declaration
c++ ×6
c ×4
arrays ×2
pointers ×2
boolean ×1
c++11 ×1
c++17 ×1
declaration ×1
printf ×1
return-type ×1
shared-ptr ×1
string ×1
string-view ×1