这是vs2015 update2的bug么

本文展示了一个关于SFINAE(Substitution Failure Is Not An Error)的有趣例子,通过模板元编程实现类型判断并选择不同的函数体。文章通过具体代码演示了如何根据不同类型的条件调用不同的函数,并展示了enable_if_t和is_arithmetic的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

难道SFINAE可以这么玩?

这居然过编译了,这是bug还是什么高深的使用方式?提交bug再说....

#include <type_traits>
#include <iostream>
using namespace std;

template<typename T>
class Class
{
public:
	Class()
	{
		bbb_ = "ABCD";
	}
	void Func(enable_if_t<is_arithmetic<T>::value>)
	{
		cout << "A" << endl;
	}
	void Func(enable_if_t<!is_arithmetic<T>::value>)
	{
		cout << "B" << endl;
	}
	enable_if_t<!is_arithmetic<T>::value> Func2()
	{
		cout << "C" << endl;
		return 123;
	}
	void Test(enable_if_t<false> val)
	{
		cout << "D" << endl;
	}
	enable_if_t<false> bbb_;
};
void Test(int val)
{
	cout << hex << val << endl;
}
void Test2(void* val)
{
	cout << val << endl;
}
int main()
{
	Class<int> test;
	test.Func();
	test.Func2();
	auto result = test.Func2();

	enable_if_t<false> a = "ABC";
	enable_if_t<false> b = 123;
	enable_if_t<false> c = result;

	auto pa = &a;
	auto pb = &b;
	auto pc = &c;
	test.bbb_ = 123;
	test.bbb_ = "ABC";

	test.Test("ABC");
	test.Test(123);
	test.Test(a == b);
	Test(b);
	Test2(b);

	typedef enable_if_t<false> T;
	T d = 123;
	T e = "DEF";
	T f = 0.1;
	T g = &test;
	T h = &Class<void>::bbb_;
	T i = a + b + c + d + e + f + g + h + 123 + "GHI" + 0.1 + &test;
	auto size = sizeof(T);
	decltype(i) j;
	auto size2 = sizeof(i);
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值