std ::有条件参数上的模板功能扣除失败
请,在将其标记为这个问题读取整个帖子
此代码无法编译,并没有模板扣除错误:
#include <iostream>
#include <type_traits>
template<typename T = float, int N>
class MyClass
{
public:
template<typename DATA_TYPE>
using MyType = std::conditional_t<(N>0), DATA_TYPE, double>;
MyType<T> Var;
void Foo()
{
Bar(Var);
}
template<typename TYPE>
void Bar(MyType<TYPE> Input)
{
std::cout << typeid(Input).name() << std::endl;
}
};
int main()
{
MyClass<float, 1> c;
c.Foo();
return 0;
}
我了解我上面链接的问题中提出的观点,也就是“条件允许选择类型的类型取决于类型本身”,但是,为什么在我提供的特定情况下,编译器会失败,因为这里的条件似乎完全独立于类型,或者我缺少某些东西?
如果有人可以参考C ++标准的一部分,我会感到非常高兴,这可以使我完全理解这种行为。
Please, before marking this as a duplicate of This question read the entirety of the post
This piece of code fails to compile, with a template deduction error:
#include <iostream>
#include <type_traits>
template<typename T = float, int N>
class MyClass
{
public:
template<typename DATA_TYPE>
using MyType = std::conditional_t<(N>0), DATA_TYPE, double>;
MyType<T> Var;
void Foo()
{
Bar(Var);
}
template<typename TYPE>
void Bar(MyType<TYPE> Input)
{
std::cout << typeid(Input).name() << std::endl;
}
};
int main()
{
MyClass<float, 1> c;
c.Foo();
return 0;
}
I understand the point that was made in the question i linked above, which is that "the condition which allows to choose the type to be deduced depends on the type itself", however, why would the compiler fail in the specific case i provided as the condition here seems to be fully independent from the type, or is there something i'm missing?
I would be more than happy if someone could refer to a section of the c++ standard that would allow me to fully understand this behaviour.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
作为链接的问题,
type
是不可推断的。myType&lt; type&gt;
实际上是xxx&lt; type&gt; ::类型
。您有几个替代方案,我想说的是
bar
不再模板:需要
(或pre-c ++ 20):As the linked question,
TYPE
is non deducible.MyType<TYPE>
is actuallyXXX<TYPE>::type
.You have several alternatives, from your code, I would say one of
Bar
no longer template:requires
(or SFINAE/specialization for pre-c++20):