Proplems understanding C++ templates.



  • I have problems understanding C++ templates. I have read that templates are for unknown types and c++ have more features than C# and since I am doing converting code I have troubles to understand whats wrong here is:

    @template<typename T>
    class IDable : public IIDable
    {
    public:
    T Data;

    IDable(const QString &id, T t)
    {
    this->id = id;
    this->Data = t;
    }

    private:
    QString id;
    const QString &getID() = IIDable->ID->get const
    {
    return id;
    }

    };

    //C# TO C++ CONVERTER TODO TASK: There is no native C++ template equivalent to generic constraints:
    template<typename TType> where TType : IIDable@

    The error message from converter sofware tells something but it is not clear issue for me as I have never used templates. How to modify this code to C++ eqvalient?


  • Moderators

    This is hardly a Qt issue.

    From a C++ template view neither line 15 nor line 23 make sense.

    @
    template<typename T>
    class IDable : public IIDable
    {
    public:
    T Data;

    IDable(const QString &id, T t)
    {
    this->id = id;
    this->Data = t;
    }

    private:
    QString id;

    const QString &getID() const
    {
        return id;
    }
    

    };

    //C# TO C++ CONVERTER TODO TASK: There is no native C++ template equivalent to generic constraints:
    // template<typename TType> where TType : IIDable
    @

    So you should be able to compile, but most likely some functionality is missing.



  • Is this quick and dirty fix for this:

    @template<typename TType> class GenericRegisterer { private: GenericRegisterer(){}};
    template<> class GenericRegisterer<IIDable> { public: GenericRegisterer(){}};@



  • It is not because TType is not defined for next class so GenerigRegister don't know variable TType.



  • //C# TO C++ CONVERTER TODO TASK: There is no native C++ template equivalent to generic constraints:
    template<typename TType> where TType : IIDable

    What is that line supposed to achieve?

    It is not because TType is not defined for next class so GenerigRegister don't know variable TType.

    Definitely not. The template< typename XX > syntax is used to tell the compiler to "build" a method / class as soon as you are using the method / class with a defined type (int, custom class, etc.). The keyword placed after typename will just be replaced by the used type at compile time and does not mean anything.


Log in to reply
 

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