std ::有条件参数上的模板功能扣除失败

发布于 2025-01-29 23:11:29 字数 1032 浏览 3 评论 0原文

请,在将其标记为这个问题读取整个帖子

此代码无法编译,并没有模板扣除错误:

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一城柳絮吹成雪 2025-02-05 23:11:29

作为链接的问题,type是不可推断的。 myType&lt; type&gt;实际上是xxx&lt; type&gt; ::类型

您有几个替代方案,我想说的是

  • bar不再模板:

     模板&lt; typename t = float,int n&gt;
    上课myllass
    {
        民众:
            template&lt; typename data_type&gt; gt;
            使用myType = std :: particyal_t&lt;(n&gt; 0),data_type,double&gt;;;
    
            mytype&lt; t&gt; var;
    
            void foo()
            {
               bar(var);
            }
    
            void bar(mytype&lt; t&gt;输入)
            {
                std :: cout&lt;&lt; typeId(input).name()&lt;&lt; std :: endl;
            }
    };
     
  • 需要(或pre-c ++ 20):

     模板&lt; typename t = float,int n&gt;
    上课myllass
    {
        民众:
            template&lt; typename data_type&gt; gt;
            使用myType = std :: partysal_t&lt;(n&gt; 0),data_type,double&gt;;
    
            mytype&lt; t&gt; var;
    
            void foo()
            {
               bar(var);
            }
    
            template&lt; typename类型&gt;
            void bar(类型输入)需要(n&gt; 0)
            {
                std :: cout&lt;&lt; typeId(input).name()&lt;&lt; std :: endl;
            }
            void bar(双输入)需要(n&lt; = 0)
            {
                std :: cout&lt;&lt; typeId(input).name()&lt;&lt; std :: endl;
            }
    };
     

As the linked question, TYPE is non deducible. MyType<TYPE> is actually XXX<TYPE>::type.

You have several alternatives, from your code, I would say one of

  • Bar no longer template:

    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);
            }
    
            void Bar(MyType<T> Input)
            {
                std::cout << typeid(Input).name() << std::endl;
            }
    };
    
  • requires (or SFINAE/specialization for pre-c++20):

    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(TYPE Input) requires(N > 0)
            {
                std::cout << typeid(Input).name() << std::endl;
            }
            void Bar(double Input) requires(N <= 0)
            {
                std::cout << typeid(Input).name() << std::endl;
            }
    };
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文