QSharedPointer::create(...) and MSVC



  • Hello!

    I'm trying to use the static method QSharedPointer::create, but MSVC is returning a C2660 (function does not take n arguments) when I use more than 1 argument.

    The line:

    QSharedPointer<TCMessage> msg = QSharedPointer<TCMessage>::create(MSG_NETWORK, LOGIN_ACCEPTED, {idUser});
    

    does not compile, with the error being:

    error: C2660: 'QSharedPointer<TCMessage>::create': function does not take 3 arguments
    

    TCMessage does have a constructor that receives 3 arguments:

    class TCMessage
    {
    public:
    	TCMessage();
    	TCMessage(QByteArray &raw);
    	TCMessage(TCMessageType type, TCMessageType subType, QVariantList args);
    	TCMessage(TCMessageType type, TCMessageType subType);
    
    	TCMessageType getType() const;
    	TCMessageType getSubType() const;
    
    	template<typename T> inline T getInfo(int info) const { return qvariant_cast<T>(infos.at(info)); }
    	QVariantList getAllInfos() const;
    
    	QByteArray toByteArray() const;
    
    private:
    	QByteArray packet;
    
    	quint8 type;
    	quint8 subType;
    	QList<QVariant> infos;
    
    	void _preparePackage();
    };
    

    I have the CONFIG += c++11 flag in my .pro, and even tried using CONFIG += c++14.

    Am I doing something wrong here? Or Is that a known issue with MSVC? I've tried this with Qt 5.9.1 in both 2015 and 2017 versions of MSVC to the same results. I didn't find anything about this.

    Thanks in advance!


  • Moderators

    I remember Qt had some stuff related to initializer lists disabled for MSVC due to bugs in the compiler. Whether these are still present in the never versions - I don't know, but try this:

    QSharedPointer<TCMessage> msg = QSharedPointer<TCMessage>::create(MSG_NETWORK, LOGIN_ACCEPTED, QVariantList{idUser});
    


  • @Chris-Kawa said in QSharedPointer::create(...) and MSVC:

    I remember Qt had some stuff related to initializer lists disabled for MSVC due to bugs in the compiler. Whether these are still present in the never versions - I don't know, but try this:

    QSharedPointer<TCMessage> msg = QSharedPointer<TCMessage>::create(MSG_NETWORK, LOGIN_ACCEPTED, QVariantList{idUser});
    

    It worked!

    But, it IS strange after all. The line

    TCMessage *msg = new TCMessage(MSG_NETWORK, LOGIN_ACCEPTED, {idUser});
    

    Will compile and run fine, while

    QSharedPointer<TCMessage> msg = QSharedPointer<TCMessage>::create(MSG_NETWORK, LOGIN_ACCEPTED, {idUser});
    

    won't.


  • Moderators

    Although parameters look the same those are two very different cases, so are subject to slightly different behavior. The shared pointer one uses perfect forwarding on top of the implicit type deduction from the first case.



  • @Chris-Kawa said in QSharedPointer::create(...) and MSVC:

    Although parameters look the same those are two very different cases, so are subject to slightly different behavior. The shared pointer one uses perfect forwarding on top of the implicit type deduction from the first case.

    True, and IIRC, it uses variadic template as well... anyway, I'll mark as solved. Thanks again!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.