蓝桥ROS机器人之现代C++学习笔记2.5 模板

Posted zhangrelay

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了蓝桥ROS机器人之现代C++学习笔记2.5 模板相关的知识,希望对你有一定的参考价值。

 


git clone https://github.com/changkun/modern-cpp-tutorial.git

coliru.stacked-crooked.com

 


 

外部模板

#include <iostream>
#include <vector>

template class std::vector<bool>;           // forcely instantiation
extern template class std::vector<double>;  // external template for avoiding instantiation in this file

template<bool T> class MagicType 
    bool magic = T;
;

int main() 
    // the >> in template
    std::vector<std::vector<int>> matrix;
    std::vector<MagicType<(1>2)>> magic; // legal, but not recommended

g++ -std=c++17 2.12.external.template.cpp -o exttemp

注意:

尖括号 “>”

std::vector<std::vector<int>> matrix;

不加-std=c++17,按传统c++模式,比如error:

编译器会给出提示的:

2.12.external.template.cpp:17:18: warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11


类型别名模板

#include <iostream>
#include <vector>
#include <string>

template<typename T, typename U>
class MagicType 
public:
    T dark;
    U magic;
;

// illegal
// template<typename T>
// typedef MagicType<std::vector<T>, std::string> FakeDarkMagic;

typedef int (*process)(void *);
using NewProcess = int(*)(void *);
template<typename T>
using TrueDarkMagic = MagicType<std::vector<T>, std::string>;

int main() 
    // FakeDarkMagic<bool> me;
    TrueDarkMagic<bool> you;

g++ -std=c++17 2.13.alias.template.cpp -o altemp

 


模板求和:

#include <iostream>

template<typename T = int, typename U = int>
auto add(T x, U y) -> decltype(x+y) 
    return x+y;


int main() 
    std::cout << add(1, 2) << std::endl;

g++ -std=c++17 2.14.default.template.param.cpp -o detemp


 


变长参数模板

#include <iostream>
#include <vector>
#include <string>

// sizeof...
template<typename... Ts>
void magic(Ts... args) 
    std::cout << sizeof...(args) << std::endl;



// 1. recursive parameter unpack
template<typename T0>
void printf1(T0 value) 
    std::cout << value << std::endl;

template<typename T, typename... Ts>
void printf1(T value, Ts... args) 
    std::cout << value << std::endl;
    printf1(args...);


// 2. variadic template parameter unfold
template<typename T0, typename... T>
void printf2(T0 t0, T... t) 
    std::cout << t0 << std::endl;
    if constexpr (sizeof...(t) > 0) printf2(t...);


// 3. parameter unpack using initializer_list
template<typename T, typename... Ts>
auto printf3(T value, Ts... args) 
    std::cout << value << std::endl;
    (void) std::initializer_list<T>([&args] 
        std::cout << args << std::endl;
    (), value)...;


int main() 
    magic();
    magic(1);
    magic(1,"");
    
    printf1(1, 2, "123", 1.1);
    printf2(1, 2.3, "abc");
    printf3(111, 123, "alpha", 1.2);
    return 0;


折叠表达式 

#include <iostream>
template<typename ... T>
auto sum(T ... t) 
    return (t + ...);

int main() 
    std::cout << sum(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) << std::endl;


非类型模板参数推导

#include <iostream>

template <auto value> void foo() 
    std::cout << value << std::endl;
    return;


int main() 
    foo<10>(); // value is deduced as type int

 


shiyanlou:2/ (master) $ g++ -std=c++17 2.12.external.template.cpp -o exttemp
shiyanlou:2/ (master*) $ ./exttemp                                   [14:39:12]
shiyanlou:2/ (master*) $ g++ 2.12.external.template.cpp -o exttemp   [14:39:16]
2.12.external.template.cpp:17:18: warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11
     bool magic = T;
                  ^
2.12.external.template.cpp: In function \\u2018int main()\\u2019:
2.12.external.template.cpp:22:32: error: \\u2018>>\\u2019 should be \\u2018> >\\u2019 within a nested template argument list
     std::vector<std::vector<int>> matrix;
                                ^
2.12.external.template.cpp:23:35: error: \\u2018magic\\u2019 was not declared in this scope
     std::vector<MagicType<(1>2)>> magic; // legal, but not recommended
                                   ^
2.12.external.template.cpp:23:35: error: template argument 1 is invalid
2.12.external.template.cpp:23:17: error: template argument 1 is invalid
     std::vector<MagicType<(1>2)>> magic; // legal, but not recommended
                 ^
2.12.external.template.cpp:23:17: error: template argument 2 is invalid
shiyanlou:2/ (master*) $ g++ -std=c++17 2.13.alias.template.cpp -o altemp
shiyanlou:2/ (master*) $ ./altemp                                    [14:46:30]
shiyanlou:2/ (master*) $ g++ -std=c++17 2.14.default.template.param.cpp -o detemp
shiyanlou:2/ (master*) $ ./detemp                                    [14:51:33]
3
shiyanlou:2/ (master*) $ g++ -std=c++17 2.15.variadic.template.param.cpp -o vartemp
2.15.variadic.template.param.cpp: In function \\u2018void printf2(T0, T ...)\\u2019:
2.15.variadic.template.param.cpp:36:8: error: expected \\u2018(\\u2019 before \\u2018constexpr\\u2019
     if constexpr (sizeof...(t) > 0) printf2(t...);
        ^
2.15.variadic.template.param.cpp: In function \\u2018auto printf3(T, Ts ...)\\u2019:
2.15.variadic.template.param.cpp:43:40: error: parameter packs not expanded with \\u2018...\\u2019:
     (void) std::initializer_list<T>([&args] 
                                        ^
2.15.variadic.template.param.cpp:43:40: note:         \\u2018args\\u2019
2.15.variadic.template.param.cpp:43:44: error: parameter packs not expanded with \\u2018...\\u2019:
     (void) std::initializer_list<T>([&args] 
                                            ^
2.15.variadic.template.param.cpp:43:44: note:         \\u2018args\\u2019
2.15.variadic.template.param.cpp:45:16: error: expansion pattern \\u2018(<lambda>(), value)\\u2019 contains no argument packs
     (), value)...;
                ^
2.15.variadic.template.param.cpp: In instantiation of \\u2018auto printf3(T, Ts ...) [with T = int; Ts = int, const char*, double]\\u2019:
2.15.variadic.template.param.cpp:55:35:   required from here
2.15.variadic.template.param.cpp:43:5: error: no matching function for call to \\u2018std::initializer_list<int>::initializer_list(<brace-enclosed initializer list>)\\u2019
     (void) std::initializer_list<T>([&args] 
     ^
In file included from /usr/include/c++/5/bits/range_access.h:36:0,
                 from /usr/include/c++/5/string:51,
                 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 2.15.variadic.template.param.cpp:10:
/usr/include/c++/5/initializer_list:66:17: note: candidate: constexpr std::initializer_list<_E>::initializer_list() [with _E = int]
       constexpr initializer_list() noexcept
                 ^
/usr/include/c++/5/initializer_list:66:17: note:   candidate expects 0 arguments, 1 provided
/usr/include/c++/5/initializer_list:62:17: note: candidate: constexpr std::initializer_list<_E>::initializer_list(std::initializer_list<_E>::const_iterator, std::initializer_list<_E>::size_type) [with _E = int; std::initializer_list<_E>::const_iterator = const int*; std::initializer_list<_E>::size_type = long unsigned int]
       constexpr initializer_list(const_iterator __a, size_type __l)
                 ^
/usr/include/c++/5/initializer_list:62:17: note:   candidate expects 2 arguments, 1 provided
/usr/include/c++/5/initializer_list:47:11: note: candidate: constexpr std::initializer_list<int>::initializer_list(const std::initializer_list<int>&)
     class initializer_list
           ^
/usr/include/c++/5/initializer_list:47:11: note:   conversion of argument 1 would be ill-formed:
/usr/include/c++/5/initializer_list:47:11: note: candidate: constexpr std::initializer_list<int>::initializer_list(std::initializer_list<int>&&)
/usr/include/c++/5/initializer_list:47:11: note:   conversion of argument 1 would be ill-formed:
shiyanlou:2/ (master*) $ ./2.15.variadic.template.param.cpp                    [14:56:06]
shiyanlou:2/ (master*) $ g++ -std=c++17 2.15.variadic.template.param.cpp -o vartemp
2.15.variadic.template.param.cpp: In function \\u2018void printf2(T0, T ...)\\u2019:
2.15.variadic.template.param.cpp:36:8: error: expected \\u2018(\\u2019 before \\u2018constexpr\\u2019
     if constexpr (sizeof...(t) > 0) printf2(t...);
        ^
2.15.variadic.template.param.cpp: In function \\u2018auto printf3(T, Ts ...)\\u2019:
2.15.variadic.template.param.cpp:43:40: error: parameter packs not expanded with \\u2018...\\u2019:
     (void) std::initializer_list<T>([&args] 
                                        ^
2.15.variadic.template.param.cpp:43:40: note:         \\u2018args\\u2019
2.15.variadic.template.param.cpp:43:44: error: parameter packs not expanded with \\u2018...\\u2019:
     (void) std::initializer_list<T>([&args] 
                                            ^
2.15.variadic.template.param.cpp:43:44: note:         \\u2018args\\u2019
2.15.variadic.template.param.cpp:45:16: error: expansion pattern \\u2018(<lambda>(), value)\\u2019 contains no argument packs
     (), value)...;
                ^
2.15.variadic.template.param.cpp: In instantiation of \\u2018auto printf3(T, Ts ...) [with T = int; Ts = int, const char*, double]\\u2019:
2.15.variadic.template.param.cpp:55:35:   required from here
2.15.variadic.template.param.cpp:43:5: error: no matching function for call to \\u2018std::initializer_list<int>::initializer_list(<brace-enclosed initializer list>)\\u2019
     (void) std::initializer_list<T>([&args] 
     ^
In file included from /usr/include/c++/5/bits/range_access.h:36:0,
                 from /usr/include/c++/5/string:51,
                 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 2.15.variadic.template.param.cpp:10:
/usr/include/c++/5/initializer_list:66:17: note: candidate: constexpr std::initializer_list<_E>::initializer_list() [with _E = int]
       constexpr initializer_list() noexcept
                 ^
/usr/include/c++/5/initializer_list:66:17: note:   candidate expects 0 arguments, 1 provided
/usr/include/c++/5/initializer_list:62:17: note: candidate: constexpr std::initializer_list<_E>::initializer_list(std::initializer_list<_E>::const_iterator, std::initializer_list<_E>::size_type) [with _E = int; std::initializer_list<_E>::const_iterator = const int*; std::initializer_list<_E>::size_type = long unsigned int]
       constexpr initializer_list(const_iterator __a, size_type __l)
                 ^
/usr/include/c++/5/initializer_list:62:17: note:   candidate expects 2 arguments, 1 provided
/usr/include/c++/5/initializer_list:47:11: note: candidate: constexpr std::initializer_list<int>::initializer_list(const std::initializer_list<int>&)
     class initializer_list
           ^
/usr/include/c++/5/initializer_list:47:11: note:   conversion of argument 1 would be ill-formed:
/usr/include/c++/5/initializer_list:47:11: note: candidate: constexpr std::initializer_list<int>::initializer_list(std::initializer_list<int>&&)
/usr/include/c++/5/initializer_list:47:11: note:   conversion of argument 1 would be ill-formed:
shiyanlou:2/ (master*) $ g++ -std=c++17 2.16.fold.expression.cpp -o foexp      [14:56:27]
2.16.fold.expression.cpp: In function \\u2018auto sum(T ...)\\u2019:
2.16.fold.expression.cpp:13:17: error: expected primary-expression before \\u2018...\\u2019 token
     return (t + ...);
                 ^
2.16.fold.expression.cpp:13:17: error: expected \\u2018)\\u2019 before \\u2018...\\u2019 token
2.16.fold.expression.cpp:13:20: error: parameter packs not expanded with \\u2018...\\u2019:
     return (t + ...);
                    ^
2.16.fold.expression.cpp:13:20: note:         \\u2018t\\u2019
2.16.fold.expression.cpp: In function \\u2018int main()\\u2019:
2.16.fold.expression.cpp:16:15: error: no match for \\u2018operator<<\\u2019 (operand types are \\u2018std::ostream aka std::basic_ostream<char>\\u2019 and \\u2018void\\u2019)
     std::cout << sum(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) << std::endl;
               ^
In file included from /usr/include/c++/5/iostream:39:0,
                 from 2.16.fold.expression.cpp:10:
/usr/include/c++/5/ostream:108:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ostream_type& (*)(std::basic_ostream<_CharT, _Traits>::__ostream_type&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
       operator<<(__ostream_type& (*__pf)(__ostream_type&))
       ^
/usr/include/c++/5/ostream:108:7: note:   no known conversion for argument 1 from \\u2018void\\u2019 to \\u2018std::basic_ostream<char>::__ostream_type& (*)(std::basic_ostream<char>::__ostream_type&) aka std::basic_ostream<char>& (*)(std::basic_ostream<char>&)\\u2019
/usr/include/c++/5/ostream:117:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ios_type& (*)(std::basic_ostream<_CharT, _Traits>::__ios_type&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>; std::basic_ostream<_CharT, _Traits>::__ios_type = std::basic_ios<char>]
       operator<<(__ios_type& (*__pf)(__ios_type&))
       ^
/usr/include/c++/5/ostream:117:7: note:   no known conversion for argument 1 from \\u2018void\\u2019 to \\u2018std::basic_ostream<char>::__ios_type& (*)(std::basic_ostream<char>::__ios_type&) aka std::basic_ios<char>& (*)(std::basic_ios<char>&)\\u2019
/usr/include/c++/5/ostream:127:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& (*)(std::ios_base&)) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
       operator<<(ios_base& (*__pf) (ios_base&))
       ^
/usr/include/c++/5/ostream:127:7: note:   no known conversion for argument 1 from \\u2018void\\u2019 to \\u2018std::ios_base& (*)(std::ios_base&)\\u2019
/usr/include/c++/5/ostream:166:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
       operator<<(long __n)
       ^
/usr/include/c++/5/ostream:166:7: note:   no known conversion for argument 1 from \\u2018void\\u2019 to \\u2018long int\\u2019
/usr/include/c++/5/ostream:170:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
       operator<<(unsigned long __n)
       ^
/usr/include/c++/5/ostream:170:7: note:   no known conversion for argument 1 from \\u2018void\\u2019 to \\u2018long unsigned int\\u2019
/usr/include/c++/5/ostream:174:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
       operator<<(bool __n)
       ^
/usr/include/c++/5/ostream:174:7: note:   no known conversion for argument 1 from \\u2018void\\u2019 to \\u2018bool\\u2019
In file included from /usr/include/c++/5/ostream:638:0,
                 from /usr/include/c++/5/iostream:39,
                 from 2.16.fold.expression.cpp:10:
/usr/include/c++/5/bits/ostream.tcc:91:5: note: candidate: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char; _Traits = std::char_traits<char>]
     basic_ostream<_CharT, _Traits>::
     ^
/usr/include/c++/5/bits/ostream.tcc:91:5: note:   no known conversion for argument 1 from \\u2018void\\u2019 to \\u2018short int\\u2019
In file included from /usr/include/c++/5/iostream:39:0,
                 from 2.16.fold.expression.cpp:10:
/usr/include/c++/5/ostream:181:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
       operator<<(unsigned short __n)
       ^
/usr/include/c++/5/ostream:181:7: note:   no known conversion for argument 1 from \\u2018void\\u2019 to \\u2018short unsigned int\\u2019
In file included from /usr/include/c++/5/ostream:638:0,
                 from /usr/include/c++/5/iostream:39,
                 from 2.16.fold.expression.cpp:10:
/usr/include/c++/5/bits/ostream.tcc:105:5: note: candidate: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char; _Traits = std::char_traits<char>]
     basic_ostream<_CharT, _Traits>::
     ^
/usr/include/c++/5/bits/ostream.tcc:105:5: note:   no known conversion for argument 1 from \\u2018void\\u2019 to \\u2018int\\u2019
In file included from /usr/include/c++/5/iostream:39:0,
                 from 2.16.fold.expression.cpp:10:
/usr/include/c++/5/ostream:192:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
       operator<<(unsigned int __n)
       ^
/usr/include/c++/5/ostream:192:7: note:   no known conversion for argument 1 from \\u2018void\\u2019 to \\u2018unsigned int\\u2019
/usr/include/c++/5/ostream:201:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
       operator<<(long long __n)
       ^
/usr/include/c++/5/ostream:201:7: note:   no known conversion for argument 1 from \\u2018void\\u2019 to \\u2018long long int\\u2019
/usr/include/c++/5/ostream:205:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
       operator<<(unsigned long long __n)
       ^
/usr/include/c++/5/ostream:205:7: note:   no known conversion for argument 1 from \\u2018void\\u2019 to \\u2018long long unsigned int\\u2019
/usr/include/c++/5/ostream:220:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
       operator<<(double __f)
       ^
/usr/include/c++/5/ostream:220:7: note:   no known conversion for argument 1 from \\u2018void\\u2019 to \\u2018double\\u2019
/usr/include/c++/5/ostream:224:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
       operator<<(float __f)
       ^
/usr/include/c++/5/ostream:224:7: note:   no known conversion for argument 1 from \\u2018void\\u2019 to \\u2018float\\u2019
/usr/include/c++/5/ostream:232:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
       operator<<(long double __f)
       ^
/usr/include/c++/5/ostream:232:7: note:   no known conversion for argument 1 from \\u2018void\\u2019 to \\u2018long double\\u2019
/usr/include/c++/5/ostream:245:7: note: candidate: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(const void*) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
       operator<<(const void* __p)
       ^
/usr/include/c++/5/ostream:245:7: note:   no known conversion for argument 1 from \\u2018void\\u2019 to \\u2018const void*\\u2019
In file included from /usr/include/c++/5/ostream:638:0,
                 from /usr/include/c++/5/iostream:39,
                 from 2.16.fold.expression.cpp:10:
/usr/include/c++/5/bits/ostream.tcc:119:5: note: candidate: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__streambuf_type*) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__streambuf_type = std::basic_streambuf<char>]
     basic_ostream<_CharT, _Traits>::
     ^
/usr/include/c++/5/bits/ostream.tcc:119:5: note:   no known conversion for argument 1 from \\u2018void\\u2019 to \\u2018std::basic_ostream<char>::__streambuf_type* aka std::basic_streambuf<char>*\\u2019
In file included from /usr/include/c++/5/iostream:39:0,
                 from 2.16.fold.expression.cpp:10:
/usr/include/c++/5/ostream:628:5: note: candidate: template<class _CharT, class _Traits, class _Tp> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&)
     operator<<(basic_ostream<_CharT, _Traits>&& __os, const _Tp& __x)
     ^
/usr/include/c++/5/ostream:628:5: note:   template argument deduction/substitution failed:
/usr/include/c++/5/ostream: In substitution of \\u2018template<class _CharT, class _Traits, class _Tp> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = void]\\u2019:
2.16.fold.expression.cpp:16:51:   required from here
/usr/include/c++/5/ostream:628:5: error: forming reference to void
/usr/include/c++/5/ostream:574:5: note: candidate: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const unsigned char*)
     operator<<(basic_ostream<char, _Traits>& __out, const unsigned char* __s)
     ^
/usr/include/c++/5/ostream:574:5: note:   template argument deduction/substitution failed:
2.16.fold.expression.cpp:16:21: note:   cannot convert \\u2018sum<int, int, int, int, int, int, int, int, int, int>(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)\\u2019 (type \\u2018void\\u2019) to type \\u2018const unsigned char*\\u2019
     std::cout << sum(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) << std::endl;
                     ^
In file included from /usr/include/c++/5/iostream:39:0,
                 from 2.16.fold.expression.cpp:10:
/usr/include/c++/5/ostream:569:5: note: candidate: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const signed char*)
     operator<<(basic_ostream<char, _Traits>& __out, const signed char* __s)
     ^
/usr/include/c++/5/ostream:569:5: note:   template argument deduction/substitution failed:
2.16.fold.expression.cpp:16:21: note:   cannot convert \\u2018sum<int, int, int, int, int, int, int, int, int, int>(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)\\u2019 (type \\u2018void\\u2019) to type \\u2018const signed char*\\u2019
     std::cout << sum(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) << std::endl;
                     ^
In file included from /usr/include/c++/5/iostream:39:0,
                 from 2.16.fold.expression.cpp:10:
/usr/include/c++/5/ostream:556:5: note: candidate: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const char*)
     operator<<(basic_ostream<char, _Traits>& __out, const char* __s)
     ^
/usr/include/c++/5/ostream:556:5: note:   template argument deduction/substitution failed:
2.16.fold.expression.cpp:16:21: note:   cannot convert \\u2018sum<int, int, int, int, int, int, int, int, int, int>(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)\\u2019 (type \\u2018void\\u2019) to type \\u2018const char*\\u2019
     std::cout << sum(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) << std::endl;
                     ^
In file included from /usr/include/c++/5/ostream:638:0,
                 from /usr/include/c++/5/iostream:39,
                 from 2.16.fold.expression.cpp:10:
/usr/include/c++/5/bits/ostream.tcc:321:5: note: candidate: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const char*)
     operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s)
     ^
/usr/include/c++/5/bits/ostream.tcc:321:5: note:   template argument deduction/substitution failed:
2.16.fold.expression.cpp:16:21: note:   cannot convert \\u2018sum<int, int, int, int, int, int, int, int, int, int>(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)\\u2019 (type \\u2018void\\u2019) to type \\u2018const char*\\u2019
     std::cout << sum(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) << std::endl;
                     ^
In file included from /usr/include/c++/5/iostream:39:0,
                 from 2.16.fold.expression.cpp:10:
/usr/include/c++/5/ostream:539:5: note: candidate: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const _CharT*)
     operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s)
     ^
/usr/include/c++/5/ostream:539:5: note:   template argument deduction/substitution failed:
2.16.fold.expression.cpp:16:51: note:   mismatched types \\u2018const _CharT*\\u2019 and \\u2018void\\u2019
     std::cout << sum(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) << std::endl;
                                                   ^
In file included from /usr/include/c++/5/iostream:39:0,
                 from 2.16.fold.expression.cpp:10:
/usr/include/c++/5/ostream:519:5: note: candidate: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, unsigned char)
     operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)
     ^
/usr/include/c++/5/ostream:519:5: note:   template argument deduction/substitution failed:
2.16.fold.expression.cpp:16:21: note:   cannot convert \\u2018sum<int, int, int, int, int, int, int, int, int, int>(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)\\u2019 (type \\u2018void\\u2019) to type \\u2018unsigned char\\u2019
     std::cout << sum(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) << std::endl;
                     ^
In file included from /usr/include/c++/5/iostream:39:0,
                 from 2.16.fold.expression.cpp:10:
/usr/include/c++/5/ostream:514:5: note: candidate: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, signed char)
     operator<<(basic_ostream<char, _Traits>& __out, signed char __c)
     ^
/usr/include/c++/5/ostream:514:5: note:   template argument deduction/substitution failed:
2.16.fold.expression.cpp:16:21: note:   cannot convert \\u2018sum<int, int, int, int, int, int, int, int, int, int>(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)\\u2019 (type \\u2018void\\u2019) to type \\u2018signed char\\u2019
     std::cout << sum(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) << std::endl;
                     ^
In file included from /usr/include/c++/5/iostream:39:0,
                 from 2.16.fold.expression.cpp:10:
/usr/include/c++/5/ostream:508:5: note: candidate: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, char)
     operator<<(basic_ostream<char, _Traits>& __out, char __c)
     ^
/usr/include/c++/5/ostream:508:5: note:   template argument deduction/substitution failed:
2.16.fold.expression.cpp:16:21: note:   cannot convert \\u2018sum<int, int, int, int, int, int, int, int, int, int>(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)\\u2019 (type \\u2018void\\u2019) to type \\u2018char\\u2019
     std::cout << sum(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) << std::endl;
                     ^
In file included from /usr/include/c++/5/iostream:39:0,
                 from 2.16.fold.expression.cpp:10:
/usr/include/c++/5/ostream:502:5: note: candidate: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, char)
     operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)
     ^
/usr/include/c++/5/ostream:502:5: note:   template argument deduction/substitution failed:
2.16.fold.expression.cpp:16:21: note:   cannot convert \\u2018sum<int, int, int, int, int, int, int, int, int, int>(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)\\u2019 (type \\u2018void\\u2019) to type \\u2018char\\u2019
     std::cout << sum(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) << std::endl;
                     ^
In file included from /usr/include/c++/5/iostream:39:0,
                 from 2.16.fold.expression.cpp:10:
/usr/include/c++/5/ostream:497:5: note: candidate: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, _CharT)
     operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c)
     ^
/usr/include/c++/5/ostream:497:5: note:   template argument deduction/substitution failed:
2.16.fold.expression.cpp:16:51: note:   deduced conflicting types for parameter \\u2018_CharT\\u2019 (\\u2018char\\u2019 and \\u2018void\\u2019)
     std::cout << sum(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) << std::endl;
                                                   ^
In file included from /usr/include/c++/5/bits/ios_base.h:46:0,
                 from /usr/include/c++/5/ios:42,
                 from /usr/include/c++/5/ostream:38,
                 from /usr/include/c++/5/iostream:39,
                 from 2.16.fold.expression.cpp:10:
/usr/include/c++/5/system_error:209:5: note: candidate: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::error_code&)
     operator<<(basic_ostream<_CharT, _Traits>& __os, const error_code& __e)
     ^
/usr/include/c++/5/system_error:209:5: note:   template argument deduction/substitution failed:
2.16.fold.expression.cpp:16:21: note:   cannot convert \\u2018sum<int, int, int, int, int, int, int, int, int, int>(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)\\u2019 (type \\u2018void\\u2019) to type \\u2018const std::error_code&\\u2019
     std::cout << sum(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) << std::endl;
                     ^
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 2.16.fold.expression.cpp:10:
/usr/include/c++/5/bits/basic_string.h:5172:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&)
     operator<<(basic_ostream<_CharT, _Traits>& __os,
     ^
/usr/include/c++/5/bits/basic_string.h:5172:5: note:   template argument deduction/substitution failed:
2.16.fold.expression.cpp:16:51: note:   mismatched types \\u2018const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>\\u2019 and \\u2018void\\u2019
     std::cout << sum(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) << std::endl;
                                                   ^
shiyanlou:2/ (master*) $ g++ -std=c++17 2.18.non.type.template.auto.cpp -o notemp
2.18.non.type.template.auto.cpp:12:16: error: \\u2018auto\\u2019 parameter not permitted in this context
 template <auto value> void foo() 
                ^
2.18.non.type.template.auto.cpp: In function \\u2018int main()\\u2019:
2.18.non.type.template.auto.cpp:18:13: error: no matching function for call to \\u2018foo()\\u2019
     foo<10>(); // value is deduced as type int
             ^
2.18.non.type.template.auto.cpp:12:28: note: candidate: template<<typeprefixerror>value> void foo()
 template <auto value> void foo() 
                            ^
2.18.non.type.template.auto.cpp:12:28: note:   template argument deduction/substitution failed:
2.18.non.type.template.auto.cpp:18:13: note: invalid template non-type parameter
     foo<10>(); // value is deduced as type int
             ^
shiyanlou:2/ (master*) $                                                       [15:03:09]

 

 

 

 

以上是关于蓝桥ROS机器人之现代C++学习笔记2.5 模板的主要内容,如果未能解决你的问题,请参考以下文章

蓝桥ROS机器人之现代C++学习笔记之路径规划

蓝桥ROS机器人之现代C++学习笔记7.3 期物

蓝桥ROS机器人之现代C++学习笔记资料

蓝桥ROS机器人之现代C++学习笔记3.1 Lambda 表达式

蓝桥ROS机器人之现代C++学习笔记7.5 内存模型

蓝桥ROS机器人之现代C++学习笔记4.3 元组