在测试SFINAE时,我发现一些我认为不起作用的东西 [英] While testing SFINAE, I found something that I don't think should work

查看:39
本文介绍了在测试SFINAE时,我发现一些我认为不起作用的东西的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从



我在想什么,但无论我是否提出抱怨

解决方案

就像我一样,使用其他整数,甚至麻烦去做我想做的事情。

抽奖太快,因此将巴里的答案标记为正确答案。但是,似乎不太正确,或者至少不清楚。



一个函数可以具有有效的重载,只要其参数不同即可。模板功能是相同的,但其签名也包括模板参数。因此,您可以使用相同的参数列表使用相同的函数名称,但使用不同的模板参数,并且仍将被视为重载。



因此,如果两个函数如果具有相同的 T 类型,则可能会发生冲突。



但是,由于存在 EnableIf 只能在互斥条件下解析为一个类型,即使第二个模板类型也可以解析为相同的 detail :: enabler ,第一个模板参数将是不同的类型(即使没有基于函数参数的自动推导且具有相同的函数参数类型,该模板仍将起作用)。因此,这将仍然是不同的函数重载。



@Barry所说的 std :: enable_if_t< std :: is_same< T,int> :: value,detail :: enabler> std :: enable_if_t< std :: is_same< T,float> :: value, detail :: enabler> 是完全无关的类型。他是正确的。有效时,它们确实解析为完全相同的类型,但他未能提及的是它们并非同时有效。


I found an interesting conditional function exclusion that I got from this site and while testing it I came across this:

#include<type_traits>

namespace detail
{
    enum enabler {};
}

template <int overload, typename Condition>
using EnableIf = std::enable_if_t<Condition::value, detail::enabler>;

template <typename T,
    EnableIf<0, std::is_same<T, int>>...>
    T twice(T t) { return 2 * t; }

template <typename T,
    EnableIf<0, std::is_same<T, float>>...>
    T twice(T t) { return 2 * t; }

int main()
{
    twice(1);
    twice(1.f);
    return 0;
}

Shouldn't this cause a compiler error because the EnableIf type are the same in both functions? I was going to use a different number for each overload and have a template class that would contain the enabler enum so that it would be a different type, but it would seem that it isn't necessary. Is this a defect or am I missing something?

I've tested this on VC++ (2017) and clang.

Although VC++ compiler doesn't complain, apparently the intellisense doesn't like it:

Which is what I thought, but it complains regardless if I use a different integer, or even go to the trouble of doing what I said I was going to do.

解决方案

Looks like I was too quick on the draw and marked Barry's answer as the correct answer. However it would seem that it is not quite right, or at least not clear.

A function can have a valid overload so long as its parameters are different. A template function is the same, but its signature also includes the template parameters. Thus, you can have the same function name with the same parameter list, but with different template parameters, and it will still be considered an overload.

So, if both functions were viable with the same type T, then there would be a collision.

However, since there is a EnableIf that would resolve to a type only under mutually exclusive conditions, then even though the 2nd template type would resolve to the same type detail::enabler, the first template parameter would be a different type (this would still work even if there was no auto deduction based on function parameters, with the same function parameter types). Thus, this would still be a different function overload.

To what @Barry said that std::enable_if_t<std::is_same<T, int>::value, detail::enabler> and std::enable_if_t<std::is_same<T, float>::value, detail::enabler> being entirely unrelated types. He is sort of correct. When valid, they do resolve to exactly the same type, but what he failed to mention was that they are just not ever valid at the same time.

这篇关于在测试SFINAE时,我发现一些我认为不起作用的东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆