标签: implicit-conversion

char *name[10] 和 char (*name)[10] 有什么区别?

我对这两个符号表示什么感到非常困惑。我知道 的优先级()大于[],这是否意味着它char(*name)[10]是一个指针并且char *name[10]是一个数组?

c arrays pointers declaration implicit-conversion

3
推荐指数
2
解决办法
406
查看次数

为什么在函数中返回 shared_ptr 时不将其隐式转换为布尔值?

以下不会编译:

#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)

标准(我认为)不允许在这里进行隐式转换的原因是什么?

c++ boolean return-type shared-ptr implicit-conversion

3
推荐指数
1
解决办法
72
查看次数

strcpy 通过字符串数组传递给函数?

长话短说,我正在制作这个 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

3
推荐指数
1
解决办法
139
查看次数

为什么转换构造函数优于转换运算符?

我有这个类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

3
推荐指数
1
解决办法
194
查看次数

如何从 std::string_view 转换为 std::string

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::stringno sutiable user-defined conversion from std::string_view to std::string exists

应该如何foo()正确打电话呢?

c++ string implicit-conversion string-view c++17

3
推荐指数
1
解决办法
2254
查看次数

在向量构造函数中从initializer_list&lt;const char*&gt; 到initializer_list&lt;string&gt; 的转换

std::vector\xe2\x80\x98s 初始值设定项列表构造函数具有以下形式

\n
vector( std::initializer_list<T> init, const Allocator& alloc = Allocator() );\n
Run 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\x98sTstd::stringstd::initializer_list<const char*>从到 的转换在哪里以及如何进行std::initializer_list<string>发生?

\n

c++ implicit-conversion c++11 stdinitializerlist braced-init-list

3
推荐指数
1
解决办法
845
查看次数

为什么 printf 中 int 到 float 转换失败?

使用 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

3
推荐指数
1
解决办法
149
查看次数

为什么可以使用函数作为 if 语句条件而不调用它?

我想知道为什么这段代码的两个版本都没有错误:

#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预期的结果。

c++ function-pointers implicit-conversion

3
推荐指数
3
解决办法
147
查看次数

删除 const 时模板中出现编译错误

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)

c++ parameter-passing implicit-conversion

3
推荐指数
1
解决办法
86
查看次数

C 函数:数组参数长度

这个问题建立在这个问题的基础上,它描述了以下内容如何等效:

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

3
推荐指数
1
解决办法
121
查看次数