我指這個link實施variadic template:爲什麼C++ variadic模板不接受iostream值作爲參數?
#include <iostream>
#include <sstream>
// base case
void doPrint(std::ostream &out) {}
template <typename T, typename... Args>
void doPrint(std::ostream &out, T t, Args... args)
{
out << t; // add comma here, see below
doPrint(out, args...);
}
int main() {
// See how it works even better than varargs?
doPrint(std::cout, "Hola", " mundo ");
return 0;
}
但是,如果我改變函數參數從基準
void doPrint(std::ostream &out)
......
template <typename T, typename... Args>
void doPrint(std::ostream &out, T t, Args... args)
價值:
void doPrint(std::ostream out)
......
template <typename T, typename... Args>
void doPrint(std::ostream out, T t, Args... args)
我得到以下編譯錯誤:
test.cpp: In function 'int main()':
test.cpp:16:40: error: 'std::basic_ostream<_CharT, _Traits>::basic_ostream(const std::basic_ostream<_CharT, _Traits>&) [with _CharT = char; _Traits = std::char_traits<char>]' is protected within this context
doPrint(std::cout, "Hola", " mundo ");
^
In file included from /usr/include/c++/7.1.1/iostream:39:0,
from test.cpp:1:
/usr/include/c++/7.1.1/ostream:391:7: note: declared protected here
basic_ostream(const basic_ostream&) = delete;
^~~~~~~~~~~~~
test.cpp:16:40: error: use of deleted function 'std::basic_ostream<_CharT, _Traits>::basic_ostream(const std::basic_ostream<_CharT, _Traits>&) [with _CharT = char; _Traits = std::char_traits<char>]'
doPrint(std::cout, "Hola", " mundo ");
^
In file included from /usr/include/c++/7.1.1/iostream:39:0,
from test.cpp:1:
/usr/include/c++/7.1.1/ostream:391:7: note: declared here
basic_ostream(const basic_ostream&) = delete;
^~~~~~~~~~~~~
test.cpp:8:6: note: initializing argument 1 of 'void doPrint(std::ostream, T, Args ...) [with T = const char*; Args = {const char*}; std::ostream = std::basic_ostream<char>]'
void doPrint(std::ostream out, T t, Args... args)
^~~~~~~
test.cpp: In instantiation of 'void doPrint(std::ostream, T, Args ...) [with T = const char*; Args = {const char*}; std::ostream = std::basic_ostream<char>]':
test.cpp:16:40: required from here
test.cpp:11:12: error: 'std::basic_ostream<_CharT, _Traits>::basic_ostream(const std::basic_ostream<_CharT, _Traits>&) [with _CharT = char; _Traits = std::char_traits<char>]' is protected within this context
doPrint(out, args...);
~~~~~~~^~~~~~~~~~~~~~
In file included from /usr/include/c++/7.1.1/iostream:39:0,
from test.cpp:1:
/usr/include/c++/7.1.1/ostream:391:7: note: declared protected here
basic_ostream(const basic_ostream&) = delete;
^~~~~~~~~~~~~
test.cpp:11:12: error: use of deleted function 'std::basic_ostream<_CharT, _Traits>::basic_ostream(const std::basic_ostream<_CharT, _Traits>&) [with _CharT = char; _Traits = std::char_traits<char>]'
doPrint(out, args...);
~~~~~~~^~~~~~~~~~~~~~
In file included from /usr/include/c++/7.1.1/iostream:39:0,
from test.cpp:1:
/usr/include/c++/7.1.1/ostream:391:7: note: declared here
basic_ostream(const basic_ostream&) = delete;
^~~~~~~~~~~~~
test.cpp:8:6: note: initializing argument 1 of 'void doPrint(std::ostream, T, Args ...) [with T = const char*; Args = {}; std::ostream = std::basic_ostream<char>]'
void doPrint(std::ostream out, T t, Args... args)
^~~~~~~
根據我的理解,使用值可能沒有很好的性能作爲參考,但它不應該導致編譯錯誤。爲什麼C++ variadic模板不接受iostream
值作爲參數?
無關變量模板BTW。 – Jarod42
你的理解是不正確的。按值傳遞涉及複製,並非所有類型都允許。 – StoryTeller
@StoryTeller我不會說「不正確」 - 它相當「不完整」。在一般情況下,傳遞值確實比較慢,因爲必須調用複製構造函數(這是缺少的難題)。 –