Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为不同的特征专门化同一个操作符_C++_C++14_Template Specialization_Typetraits - Fatal编程技术网

C++ 为不同的特征专门化同一个操作符

C++ 为不同的特征专门化同一个操作符,c++,c++14,template-specialization,typetraits,C++,C++14,Template Specialization,Typetraits,我想通过特质的专业化来做以下工作 数组Aa=a中的标量将使用重载I Array Aa=Array Bb将使用重载II 在下面的代码中,重载II永远不会被使用 有人提到,T1不能在overload II中推导 如何解决这个问题 我用C++ shell编译代码,用C++ 14。< /P> #include <iostream> #include <type_traits> using namespace std; class A; // forward declaratio

我想通过特质的专业化来做以下工作

  • 数组Aa=a中的标量将使用
    重载I

  • Array Aa=Array Bb
    将使用
    重载II

  • 在下面的代码中,
    重载II
    永远不会被使用

    有人提到,
    T1
    不能在
    overload II
    中推导

    如何解决这个问题

    我用C++ shell编译代码,用C++ 14。< /P>

    #include <iostream>
    #include <type_traits>
    
    using namespace std;
    class A; // forward declaration.
    
    template <typename T>
    struct is_A : false_type {};
    template <> struct is_A<A> : true_type {};
    
    template <typename T>
    struct is_int : false_type {};
    template <> struct is_int<int> : true_type {};
    template <> struct is_int<long> : true_type {};
    
    class A{
        public:
            int val;
            void print(void){
                std::cout << val << std::endl;
            }
            template <typename T1>
            enable_if_t<is_int<T1>::value,void>
            operator=(const T1 & input){
                val = 2*input; //Overload I
            }
            template <typename T1>
            enable_if_t<is_A<T1>::value,void>
            operator=(const T1 & Bb){
                val = 5*Bb.val; //Overload II
            }
    };
    
    int main(void){
        A Aa;
        A Bb;
        int in_a = 3;
        Aa = in_a; //This uses overload I as intended.
        Bb = Aa; //I want this to use overload II, but
                 //actually overload I is used.
                 //This leads to an error during compilation.
        Aa.print(); //This should give 6. (3x2)
        Bb.print(); //This should give 30. (6x5)
    }
    
    #包括
    #包括
    使用名称空间std;
    A类;//远期申报。
    模板
    结构是_A:false _type{};
    模板结构是_A:true _type{};
    模板
    结构为_int:false _type{};
    模板结构为_int:true _type{};
    模板结构为_int:true _type{};
    甲级{
    公众:
    int-val;
    作废打印(作废){
    std::cout您的代码应该是

    template <typename T>
    std::enable_if_t<is_int<T>::value, A&>
    operator=(const T& input){
        val = 2 * input; //Overload I
        return *this;
    }
    template <typename T>
    std::enable_if_t<is_A<T>::value, A&>
    operator=(T& rhs){
        val = 5 * rhs.val; //Overload II
        return *this;
    }
    

    以下是您的简化代码并按预期工作:

    #include <iostream>
    #include <type_traits>
    #include<utility>
    
    class A;
    
    template <typename T>
    struct is_A : std::false_type {};
    template <> struct is_A<A> : std::true_type {};
    
    template <typename T>
    struct is_int : std::false_type {};
    template <> struct is_int<int> : std::true_type {};
    template <> struct is_int<long> : std::true_type {};
    
    class A{
    public:
        int val;
    
        void print(void){
            std::cout << val << std::endl;
        }
    
        template <typename T1>
        std::enable_if_t<is_int<std::decay_t<T1>>::value, void>
        operator=(T1 && input){
            val = 2*std::forward<T1>(input);
        }
    
        template <typename T1>
        std::enable_if_t<is_A<std::decay_t<T1>>::value,void>
        operator=(T1 && Bb){
            val = 5*std::forward<T1>(Bb).val;
        }
    };
    
    int main(void){
        A Aa;
        A Bb;
        int in_a = 3;
        Aa = in_a;
        Bb = Aa;
        Aa.print(); //This should give 6. (3x2)
        Bb.print(); //This should give 30. (6x5)
    }
    
    #包括
    #包括
    #包括
    甲级;
    模板
    结构是_A:std::false_type{};
    模板结构是_A:std::true_type{};
    模板
    结构是_int:std::false_type{};
    模板结构是_int:std::true_type{};
    模板结构是_int:std::true_type{};
    甲级{
    公众:
    int-val;
    作废打印(作废){
    
    std::cout对于您的简单案例,您真的需要所有的模板魔法吗

    #include <iostream>
    
    class A;
    
    class A{
    public:
        int val;
    
        void print(void){
            std::cout << val << std::endl;
        }
    
        void operator =(const A& in){ val = in.val*5; }
        void operator =(int in) { val = in*2; }
    };
    
    
    int main(void){
        A Aa;
        A Bb;
        Aa = 3;
        Bb = Aa;
        Aa.print(); //This should give 6. (3x2)
        Bb.print(); //This should give 30. (6x5)
        return 0;
    }
    
    #包括
    甲级;
    甲级{
    公众:
    int-val;
    作废打印(作废){
    
    std::您是否有
    操作符=(A&Bb)
    而没有
    T1
    …但是简单的重载在您的情况下似乎就足够了。
    操作符=
    应该返回
    A&
    并获取常量引用。这是固定的(希望正确)但是现在编译器又抱怨了。<代码> Enable,如果<代码> Enable,IFIFT 也。我绝对需要<代码> Enable IFIFT < /COD>。因为代码是一个简化的实际代码版本。非常感谢答案。我将在实际代码上测试它。这里是C++外壳的编译结果…<代码>错误:“Enable IFIFT”。“在命名空间“std”中,不命名模板类型
    如果为c++14编译,则运行编译的程序会给出
    6 6
    而不是
    6 30
    。副本分配不应是模板,您必须添加
    a&运算符=(const a&)
    @skypjack:的确,这不是必需的,但这是一种很好的做法。因为
    操作符==
    可能会返回
    std::string
    ,而不是预期的
    bool
    。谢谢你的回答。我需要定义我自己的特征,但它不会起作用。我正在为此发布另一个问题。@rxu好吧,答案仍然适用。使用你自己的特征它也会起作用。实际情况是float、int、short等将被视为标量。矩阵类型A、矩阵类型B、矩阵类型C将被视为_A…我有一个古老的标准库。我只能从最新的标准库复制一些东西。我想我下次简化代码时会对简化的内容进行注释。。。我的不好。“寻找
    衰变的实现”
    “这是简化的代码。我需要模板和实际代码的特征专门化。int、float、double等有许多类型的数组以及许多类型的标量。需要它们之间的数学模板。可能是。但是,很难理解整个pro代码和平带来的问题。你是说像向量求和、乘法之类的数学吗?就像在matlab中一样?是的。数学就像在python的numpy库中一样,也是在matlab中。这对我来说很有趣,因为我不久前做了一个基于模板的矩阵数学库。如果你使用模板,那么你可以删除循环。我的意思是如果你使用向量元素例如,像两个向量(Vector)求和这样的明智操作,您不需要在(int i=0;i#include <iostream> class A; class A{ public: int val; void print(void){ std::cout << val << std::endl; } void operator =(const A& in){ val = in.val*5; } void operator =(int in) { val = in*2; } }; int main(void){ A Aa; A Bb; Aa = 3; Bb = Aa; Aa.print(); //This should give 6. (3x2) Bb.print(); //This should give 30. (6x5) return 0; }