首页 > 其他 > 详细

const string & pk string &

时间:2021-04-21 16:08:05      阅读:41      评论:0      收藏:0      [点我收藏+]

 

 

#include<iostream>
#include<string>
using namespace std;
template<typename T1, typename T2>
void set(T1 && var1, T2 && var2){
         T1  m_var1 = std::forward<T1>(var1);
         T2 m_var2 = std::forward<T2>(var2);
}


void set2(string && var1, string && var2){
}
void set3(const string & var1, const string & var2){
}
int main()
{
        string str1("hello");
        string str2("world");
        set(str1, str2);
        set("temporary str1","temporary str2");
        set3("temporary str1","temporary str2");
        //set2(str1, str2);
        return 0;
}
 

 

编译没问题

 

 

root@ubuntu:~/c++# vi rvalue3.cpp  +21
#include<iostream>
#include<string>
using namespace std;
template<typename T1, typename T2>
void set(T1 && var1, T2 && var2){
         T1  m_var1 = std::forward<T1>(var1);
         T2 m_var2 = std::forward<T2>(var2);
}


void set2(string && var1, string && var2){
}
void set3(const string & var1, const string & var2){
}
void set4(string & var1, string & var2){
}
int main()
{
        string str1("hello");
        string str2("world");
        set(str1, str2);
        set("temporary str1","temporary str2");
        set3("temporary str1","temporary str2");
        set4("temporary str1","temporary str2");
        //set2(str1, str2);
        return 0;
}

 

 

value3.cpp: In function ‘int main()’:
rvalue3.cpp:24:40: error: invalid initialization of non-const reference of type ‘std::__cxx11::string& {aka std::__cxx11::basic_string<char>&}’ from an rvalue of type ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’
  set4("temporary str1","temporary str2");
                                        ^
In file included from /usr/include/c++/5/string:52:0,
                 from /usr/include/c++/5/bits/locale_classes.h:40,
                 from /usr/include/c++/5/bits/ios_base.h:41,
                 from /usr/include/c++/5/ios:42,
                 from /usr/include/c++/5/ostream:38,
                 from /usr/include/c++/5/iostream:39,
                 from rvalue3.cpp:1:
/usr/include/c++/5/bits/basic_string.h:455:7: note:   after user-defined conversion: std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]
       basic_string(const _CharT* __s, const _Alloc& __a = _Alloc())
       ^
rvalue3.cpp:15:6: note:   initializing argument 1 of ‘void set4(std::__cxx11::string&, std::__cxx11::string&)’
 void set4(string & var1, string & var2){
      ^

 

const string & pk string &

原文:https://www.cnblogs.com/dream397/p/14684675.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!