我是第一名 发表于 2022-1-24 22:40:40

返回数组的引用

#include <iostream>
#include <string>
using namespace std;
string (& str(string s));

typedef string sss;
sss& str1(string s);

auto str2(string s)->string(&);

string s1;
decltype(s1)& str3(string s);



大佬看看是否都是等价的函数声明

人造人 发表于 2022-1-25 00:49:34

人造人 发表于 2022-1-25 01:32:47

$ cat main.cpp
#include <iostream>
#include <string>

//using namespace std;
using std::string;

string (&str(string s));

typedef string sss;
sss &str1(string s);

auto str2(string s) -> string (&);

string s1;
decltype(s1) &str3(string s);

using std::cout, std::endl;

int main() {
    cout << typeid(str).name() << endl;
    cout << typeid(str1).name() << endl;
    cout << typeid(str2).name() << endl;
    cout << typeid(str3).name() << endl;
    return 0;
}
$ g++-debug -o main main.cpp
$ ./main
FRA10_NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES4_E
FRA10_NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES4_E
FRA10_NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES4_E
FRA10_NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES4_E
$ ./main | c++filt -t
std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > (&(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >))
std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > (&(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >))
std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > (&(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >))
std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > (&(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >))
$
页: [1]
查看完整版本: 返回数组的引用