一个false_type的编译错误,帮忙看看哈

ewrewqr2 2015-10-08 06:13:05
下面的代码在gcc4.7下面编译不过

#include <type_traits>
using namespace std;
template<class T>
void g(T&& t, std::false_type)
{
}
template<class T>
void f(T&& t)
{
g(forward<T>(t), std::is_integral<typename remove_reference<T>::type>());
}
int main() {
int i = 1;
f(i);
return 0;
}

错误提示:
||=== Build: Debug in my (compiler: GNU GCC Compiler) ===|
C:\Users\engineer\Documents\my\main.cpp||In instantiation of 'void f(T&&) [with T = int&]':|
C:\Users\engineer\Documents\my\main.cpp|15|required from here|
C:\Users\engineer\Documents\my\main.cpp|11|error: no matching function for call to 'g(int&, std::is_integral<int>)'|
C:\Users\engineer\Documents\my\main.cpp|11|note: candidate is:|
C:\Users\engineer\Documents\my\main.cpp|5|note: template<class T> void g(T&&, std::false_type)|
C:\Users\engineer\Documents\my\main.cpp|5|note: template argument deduction/substitution failed:|
C:\Users\engineer\Documents\my\main.cpp|11|note: cannot convert 'std::is_integral<int>()' (type 'std::is_integral<int>') to type 'std::false_type {aka std::integral_constant<bool, false>}'|
||=== Build failed: 1 error(s), 2 warning(s) (0 minute(s), 0 second(s)) ===|

我的代码错在哪里呢?
...全文
135 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
ri_aje 2015-10-09
  • 打赏
  • 举报
回复
false_type 的定义是 std::integral_constant<bool,false>(),用这个就能匹配了。
  • 打赏
  • 举报
回复
没写 true_type 的重载啊
mujiok2003 2015-10-08
  • 打赏
  • 举报
回复
is this what you want?

#include <type_traits>

template<class T>
void g(T&& t, 
typename std::enable_if<std::is_integral<typename std::remove_reference<T>::type>::value, int>::type dummy= 0)
{
}
int main() {
    int i = 1;
    g(i);
    g(4L);
    //g(2.5); //error,not integral
    //g("hello world"); //error,not integral
    return 0;
}


mujiok2003 2015-10-08
  • 打赏
  • 举报
回复
what's your intention?
dustpg 2015-10-08
  • 打赏
  • 举报
回复
#include <type_traits>

template<class T> 
void g(T t, std::true_type) { 

}

template<class T> void f(T&& t) {
    using my_booltype = std::integral_constant<bool, std::is_integral<typename std::remove_reference<T>::type>::value>;
    g(std::forward<T>(t), my_booltype());
}

int main() {
    int i = 1;
    f(std::move(i));
    return 0;
}
不知道lz干嘛, 还是我自己搞错了. f函数的参数T&&, forward<T>(t)返回类型是T, is_integral<xxx>又不是返回true/false_type. g的参数却是(T&&, false_type)

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧