首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >std::endl和可变模板

std::endl和可变模板
EN

Stack Overflow用户
提问于 2014-01-02 09:12:18
回答 1查看 575关注 0票数 10

代码:

代码语言:javascript
复制
#include <iostream>

void out()
{
}

template<typename T, typename... Args>
void out(T value, Args... args)
{
    std::cout << value;
    out(args...);
}

int main()
{
    out("12345", "  ", 5, "\n"); // OK
    out(std::endl);              // compilation error
    return 0;
}

构建错误:

代码语言:javascript
复制
g++ -O0 -g3 -Wall -c -fmessage-length=0 -std=c++11 -pthread -MMD -MP -MF"main.d" -MT"main.d" -o "main.o" "../main.cpp"
../main.cpp: In function ‘int main()’:
../main.cpp:17:15: error: no matching function for call to ‘out(<unresolved overloaded function type>)’
../main.cpp:17:15: note: candidates are:
../main.cpp:3:6: note: void out()
../main.cpp:3:6: note:   candidate expects 0 arguments, 1 provided
../main.cpp:8:6: note: template<class T, class ... Args> void out(T, Args ...)
../main.cpp:8:6: note:   template argument deduction/substitution failed:
../main.cpp:17:15: note:   couldn't deduce template parameter ‘T’

所以,除了std::endl,一切都很好。我如何解决这个问题(除了使用"\n")?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-01-02 09:17:08

std::endl是一个重载的函数(在许多STL实现中是一个模板),编译器没有选择的信息。

只是把它变成static_cast<std::ostream&(*)(std::ostream&)>(std::endl)

票数 12
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20879611

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档