小编oma*_*mar的帖子

为什么编译器更喜欢f(const void*)到f(const std :: string&)?

考虑以下代码:

#include <iostream>
#include <string>

// void f(const char *) { std::cout << "const char *"; } // <-- comment on purpose
void f(const std::string &) { std::cout << "const std::string &"; }
void f(const void *) { std::cout << "const void *"; }

int main()
{
    f("hello");
    std::cout << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

我编译了这个程序g++ (Ubuntu 6.5.0-1ubuntu1~16.04) 6.5.0 20181026:

$ g++ -std=c++11 strings_1.cpp -Wall
$ ./a.out

const void *
Run Code Online (Sandbox Code Playgroud)

请注意,注释是为了测试而存在,否则编译器使用f(const char *).

那么,为什么编译器会f(const void*)接管f(const std::string …

c++ overloading stdstring string-literals overload-resolution

46
推荐指数
1
解决办法
1261
查看次数

评论中的Trigraphs,用c ++ 11转换,在c ++ 17中被忽略

考虑以下代码(注意注释):

#include <iostream>

int main()
{
    int x = 1; // <-- Why??/
    x += 1;
    std::cout << x << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

要编译这个程序,我使用的是GNU C++编译器g++:

$ g++ --version // g++ (Ubuntu 6.5.0-1ubuntu1~16.04) 6.5.0 20181026
Run Code Online (Sandbox Code Playgroud)

现在,在为C++ 11C++ 17编译时,我会得到不同的结果(和警告).

对于C++ 11 g++ -std=c++11 trigraph.cpp -Wall,:

trigraph.cpp:5:26: warning: trigraph ??/ converted to \ [-Wtrigraphs]
         int x = 1; // <-- Why??/

trigraph.cpp:5:16: warning: multi-line comment [-Wcomment]
         int x = 1; // <-- Why??/
                    ^
$ ./a.out
1 …
Run Code Online (Sandbox Code Playgroud)

c++ c++11 c++17

8
推荐指数
2
解决办法
210
查看次数

无法从lambda函数中推导出模板参数std :: function

在使用C++浏览模板时,我偶然发现了以下代码中的示例:

#include <iostream>
#include <functional>

template <typename T>
void call(std::function<void(T)> f, T v)
{
    f(v);
}

int main(int argc, char const *argv[])
{
    auto foo = [](int i) {
        std::cout << i << std::endl;
    };
    call(foo, 1);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

要编译这个程序,我使用的是GNU C++编译器 g ++:

$ g++ --version // g++ (Ubuntu 6.5.0-1ubuntu1~16.04) 6.5.0 20181026
Run Code Online (Sandbox Code Playgroud)

编译C++ 11后,我收到以下错误:

$ g++ -std=c++11 template_example_1.cpp -Wall

template_example_1.cpp: In function ‘int main(int, const char**)’:
template_example_1.cpp:15:16: error: no matching function for call to ‘call(main(int, const …
Run Code Online (Sandbox Code Playgroud)

c++ lambda templates c++11 c++17

2
推荐指数
1
解决办法
236
查看次数

函数调用期间指针可以转换为数组吗?

考虑以下代码:

#include <iostream>
#include <typeinfo>

void use_pointer(int *ptr)
{
    std::cout << typeid(ptr).name() << std::endl;
}

void use_array(int arr[])
{
    std::cout << typeid(arr).name() << std::endl;
}

int main()
{
    int *ptr = nullptr;
    // std::cout << typeid(int *).name() << std::endl; // output: Pi
    int arr[1];
    // std::cout << typeid(int[]).name() << std::endl; // output: A_i

    use_pointer(arr);
    use_array(ptr);
}
Run Code Online (Sandbox Code Playgroud)

使用g++ 6.5.0输出编译此程序:

$ g++ -std=c++11 arrays_2.cpp -Wall
$ ./a.out

Pi
Pi
Run Code Online (Sandbox Code Playgroud)

现在,当调用use_pointer(arr)阵列被衰减到的指针.(这是正确的吗?"衰变"这个词对我来说是新的.)

C++标准在[conv.array#1]中说:

可以将"数组的NT"或"未知的T的数组"类型的左值或右值转换为"指向T的指针"的prvalue.应用临时实现转换([conv.rval]).结果是指向数组的第一个元素的指针.

我想我明白我的"数组int"转换为"指向int的指针" …

c++ arrays pointers type-conversion

2
推荐指数
1
解决办法
58
查看次数