ksl
ksl

Reputation: 4709

Compiler error when constructing a vector of pairs

Please can someone help explain why I get an error when compiling the following code using Xcode 5.1 on OS X. Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn).

I want construct X below, passing it a vector of pairs.

#include <iostream>
#include <string>
#include <vector>
#include <utility>

struct X
{
public:
    typedef std::vector<std::pair<std::string, std::string>> VectorType;

    X(VectorType& params) : m_params(params)
    {
    }

    VectorType m_params;
};

int main(int argc, const char * argv[])
{
    X::VectorType pairs
    {
        { "param-1", "some-string-1"},    // pair 0
        { "param-2", "some-string-2"},    // pair 1
        { "param-3", "some-string-3"},    // pair 2
        { "param-4", "some-string-4"},    // pair 3
        { "param-5", "some-string-5"},    // pair 4
        { "param-6", "some-string-6"},    // pair 5
        { "param-7", "some-string-7"}     // pair 6
    };

    X x
    {
        {pairs[0], pairs[2], pairs[5]}
    };

    return 0;
}

The error reported is:

/main.cpp:37:7: error: no matching constructor for initialization of 'X'
    X x
      ^
/main.cpp:6:8: note: candidate constructor (the implicit move constructor) not viable: cannot convert initializer list argument to 'X'
struct X
       ^
/main.cpp:6:8: note: candidate constructor (the implicit copy constructor) not viable: cannot convert initializer list argument to 'const X'
struct X
       ^
/main.cpp:11:5: note: candidate constructor not viable: cannot convert initializer list argument to 'VectorType &' (aka 'vector<std::pair<std::string, std::string> > &')
    X(VectorType& params) : m_params(params)
    ^
1 error generated.

Upvotes: 0

Views: 1451

Answers (2)

Mike Seymour
Mike Seymour

Reputation: 254451

Your constructor should take its argument by const reference

X(VectorType const & params)
             ^^^^^

Otherwise, you can't pass a temporary vector (as you try to do), since temporaries can't bind to non-const lvalue references.

Upvotes: 4

Deduplicator
Deduplicator

Reputation: 45654

X has 3 constructors:

  • Your user-defined one, which suppresses the automatic default-ctor:

    X(VectorType& params)
    
  • The automatic copy-ctor and move-ctor:

    X(X&&) noexcept
    X(const X&)
    

The custom one expects an lvalue, never an xvalue or a constant object.
You probably want to split the ctor thus:

X(const VectorType& params) : m_params(params) {}
X(VectorType&& params) : m_params(std::move(params)) {}

Upvotes: 3

Related Questions