简体   繁体   中英

error C2783: could not deduce template argument for structure argument

I am facing error C2783. I reproduce error with similar structure test case.
Here is test case:

#include <iostream>

namespace ns1 {
    template <class T> class class_1 {};
}

namespace ns2 {
    using namespace ns1;
    template <typename T> inline ns1::class_1<T> myfunc();

    template<typename T>
    inline ns1::class_1<T> myfunc() {
            int a,b;
            std::cin>>a;
            std::cin>>b;
            if(a<b) return true;
            else return false;
    }

}


namespace ns3 {
struct myStruct {
    ns1::class_1<double> var1;
    ns1::class_1<double> var2;
    myStruct ( const ns1::class_1<double>& cl0= ns2::myfunc<double>(),
                    const ns1::class_1<double>& cl1= ns2::myfunc<double>()): var1(cl0), var2(cl1) {};
    };
}

Error is :
error C2783: 'ns1::class_1 ns2::myfunc(void)' : could not deduce template argument for 'T'

Also i wonder why its giving error for line 27 (cl0) but not line 28 (for cl1)? If I try to use this on some function its works fine only giving error when using in structure arguments.

This is a compiler bug. If you replace the contents of myfunc with valid code (as suggested), it still doesn't work. For a description, status (and acknowledgement) of the bug, see Microsoft Connect . You might try to use a helper type to get argument deduction (which works):

namespace ns1 {
    template <class T> class class_1 {
    public: class_1 (int a, int b){}
    };
}

namespace ns2 {
    template<class T> struct deduction_helper{};

    using namespace ns1;
    template <typename T> inline ns1::class_1<T> myfunc(deduction_helper<T>);

    template<typename T>
    inline ns1::class_1<T> myfunc(deduction_helper<T>) {
        int a,b;
        std::cin>>a;
        std::cin>>b;
        ns1::class_1<T> c(a,b); return c;
    }

}


namespace ns3 {
    struct myStruct {
        ns1::class_1<double> var1;
        ns1::class_1<double> var2;

        myStruct ( const ns1::class_1<double>& cl0= ns2::myfunc(ns2::deduction_helper<double>()),
                   const ns1::class_1<double>& cl1= ns2::myfunc(ns2::deduction_helper<double>())
                 ): var1(cl0), var2(cl1) {};
    };
}

int main()
{
    ns3::myStruct x;
}

NB as the helper type resides in ns2 , you could use ADL instead of qualifying the name myfunc .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM