无用知识研究:在trailing return type利用decltype,comma operator在对函数进行sfinae原创 [二]

一个可以正确编译运行的例子,但并不是trailing return:

cpp 复制代码
#include <iostream>

template<typename T, typename std::enable_if_t<std::is_integral<T>::value,int> * = nullptr >
    auto foo(T)
    -> void
{
    std::cout << "I'm an integer!\n";
}

template<typename T, typename std::enable_if_t<std::is_floating_point<T>::value, double>* = nullptr >
auto foo(T)
    -> void
{
    std::cout << "I'm a floating point number!\n";
}

int main() 
{
    foo(5);
    foo(3.4);
}


运行结果:
I'm an integer!
I'm a floating point number!

一个会出现编译错误的例子,注意foo1的写法,为什么不对?:

cpp 复制代码
#include <iostream>

template<typename T>
auto foo1(T) ->decltype(std::is_integral<T>(), void{})
{
    std::cout << "I'm an integer!\n";
}

template<typename T>
auto foo1(T) ->decltype(std::is_floating_point<T>(), void{})
{
    std::cout << "I'm a floating point number!\n";
}

int main() 
{
    foo1(5);
    foo1(3.4);
}

编译错误信息:
C2668	"foo1": 对重载函数的调用不明确
E0308	有多个 重载函数 "foo1" 实例与参数列表匹配:	


错误点大概有:
1、void{}要改成void()
2、std::is_integral<T>()的类型,有T的替换过程(也就是‌substitution‌),对于foo1(5),
类型就是std::is_integral<int>,
也就是说第一个foo1的返回类型是(std::is_integral<int>,void),也就是void。
但没有像enable_if这样有个推导过程。
所以对于第二个foo1,按照上面的推导过程,它的返回类型也是void,没有推导,
所以才会有提示:
C2668	"foo1": 对重载函数的调用不明确

编译信息如下,主要是foo1带来的,foo没问题:

看个正确写法的例子:

豆包搜:举一个C++的例子:在trailing return里利用decltype判断为浮点数的sfinae的例子

cpp 复制代码
#include <iostream>

#include <type_traits>  // 包含is_floating_point和enable_if

// 重载1:当T是浮点数时,此函数有效(通过SFINAE筛选)
template <typename T>
auto is_float(T value) -> decltype(
    // 逗号表达式:先检查enable_if的条件,再取value的类型作为返回值类型
    std::enable_if_t<std::is_floating_point_v<T>>(),  // 若T不是浮点数,这里会替换失败
    value  // decltype的最终结果是T(即函数返回值类型为T)
    ) {
    std::cout << "类型是浮点数,值为: " << value << std::endl;
    return value;
}

// 重载2:当T不是浮点数时,此函数有效(通过SFINAE筛选)
template <typename T>
auto is_float(T value) -> decltype(
    std::enable_if_t<!std::is_floating_point_v<T>>(),  // 若T是浮点数,这里会替换失败
    value
    ) {
    std::cout << "类型不是浮点数,值为: " << value << std::endl;
    return value;
}

int main() {
    // 测试浮点数类型
    is_float(3.14f);    // float(浮点数)
    is_float(3.14);     // double(浮点数)
    is_float(0.0L);     // long double(浮点数)

    // 测试非浮点数类型
    is_float(42);       // int(非浮点数)
    is_float('a');      // char(非浮点数)
    is_float(true);     // bool(非浮点数)

    return 0;
}
相关推荐
py有趣4 小时前
LeetCode算法学习之两数之和 II - 输入有序数组
学习·算法·leetcode
夏鹏今天学习了吗4 小时前
【LeetCode热题100(62/100)】搜索二维矩阵
算法·leetcode·矩阵
缺点内向5 小时前
C#: 高效移动与删除Excel工作表
开发语言·c#·.net·excel
老前端的功夫5 小时前
Web应用的永生之术:PWA落地与实践深度指南
java·开发语言·前端·javascript·css·node.js
吃着火锅x唱着歌6 小时前
LeetCode 1128.等价多米诺骨牌对的数量
算法·leetcode·职场和发展
ᐇ9596 小时前
Java HashMap深度解析:数据结构、原理与实战指南
java·开发语言·数据结构
十八岁讨厌编程6 小时前
【算法训练营 · 补充】LeetCode Hot100(中)
算法·leetcode
橘颂TA6 小时前
【剑斩OFFER】算法的暴力美学——最小覆盖字串
算法·c/c++·就业
wearegogog1236 小时前
基于混合蛙跳算法和漏桶算法的无线传感器网络拥塞控制与分簇新方法
网络·算法
QT 小鲜肉6 小时前
【个人成长笔记】在 Linux 系统下撰写老化测试脚本以实现自动压测效果(亲测有效)
linux·开发语言·笔记·单片机·压力测试