Search code examples
c++qtinitializer-listqvector

how to write nested Initialiser list, eg a QVector of QPairs


Warning:

qvector.h(509): warning C4127: conditional expression is constant (main.cpp) qvector.h(506) : while compiling class template member function 'QVector>::QVector(std::initializer_list)' being compiled with [T=QPair]

see reference to function template instantiation 'QVector>::QVector(std::initializer_list)' being compiled

my code:

QVector< QPair< double, float> > container(
        {{40, 0.0f},
         {70, 0.95f},
         {80, 0.7f}}
);

It builds fine, I just want the warnings gone (without changing the warning level). What am I doing wrong?

/* edit */

Does no one have any comments on what the warning is? what it exactly means?, whether other people have the warning? I looked up the QT documentation on initializer lists for QVectors and it didn't say much. Does no one have anything to say in general about function template instantiation?


Solution

  • The warning seems unavoidable and less to do with my initialiser-list, more to do with qvector. Any constructor for Qvector checks

      if (QTypeInfo<T>::isComplex)
    

    This is a simple enum value, there are a couple classes with no template, template *, or template in the syntax of the class name declaration listed below. Would love to have that explained to me!

    template <typename T>
    class QTypeInfo { enumbla;}
    
    template<>
    class QTypeInfo<void> { enumbla;}
    
    template <typename T>
    class QTypeInfo<T*> { enumbla;}
    

    So the if looks up the enum value for the appropriate QTypeInfo class, which is a constant as its an enum yay!

    Yay unavoidable warnings, more things to suppress!