0

Compile problem error: no matching constructor for initialization of 'std::vector'

The code base I am building has several objects that do not need to be variadic template parameters. I wanted to make them accept a vector of std::any. My objects are named after the HTML entities, such as H1, H2, PARAGRAPH.

The interface for object creation.

    template <class TYPE>
    auto _createElement(const std::vector<std::any> &attrs) -> TYPE & {
      std::unique_ptr<TYPE> e = std::make_unique<TYPE>(attrs);
      ViewManager::elements.push_back(std::move(e));
      return static_cast<TYPE &>(*ViewManager::elements.back().get());
    }

    template <class TYPE, typename... ATTRS>
    auto createElement(const ATTRS &... attribs) -> TYPE & {
    std::vector<std::any> attrvector{attribs...};
    return _createElement<TYPE>(attrvector);
    }

The template parameter pack expansion into the vector on the createElement function is not compiling. The version I am using is c++17

When I call the template function, I am passing attribute objects to it. One within the template parameter that is likened to an HTML entity name, but all capitalized. And within the parameter pack are the attributes. The attributes are objects as well.

For example, the following is defined within the template header file viewManager.hpp

    using PARAGRAPH = class PARAGRAPH : public Element {
      public:
      PARAGRAPH(const std::vector<std::any> &attribs)
          : Element({listStyleType::disc, marginTop{1_em}, marginLeft{1_em},
                     marginBottom{0_em}, marginRight{0_em}}) {
        setAttribute(attribs);
      }
    };

And in the application, like main.cpp

    auto &mainArea = createElement<DIV>(
        indexBy{"mainArea"}, objectTop{10_pct}, objectLeft{10_pct},
        objectWidth{90_pct}, objectHeight{90_pct}, textColor{50, 50, 50},
        background{100, 200, 200}, textFace{"FiraMono-Regular"}, 
        textSize{20_pt}, textWeight{400});

As you can see, the syntax uses the user defined literals which return a numericFormat object.

The complete source as I have it so far can be seen at C++ Source. I want the any object to contain the data, not a pointer as you mentioned.

1 Answers1

1

I believe that your issue is entirely dependent on the behavior of _createElement, which I presume is some library function you are using. Slightly changing your code to just return attrvector shows no issues:

#include <iostream>
#include <vector>
#include <any>

template <typename... ATTRS>
std::vector<std::any> createElement(const ATTRS &... attribs) {
std::vector<std::any> attrvector{attribs...};
return attrvector;
}

using namespace std;
int main(int argc, char *argv[]) {
    auto vec = createElement(1.0f,2.0f,3u,4u,-1,"hello");
    for (const auto& z : vec) {
        std::cout << z.type().name() << std::endl;
    }
}

prints (using a C++17 compatible version of clang++)

f
f
j
j
i
PKc
jwimberley
  • 1,696
  • 11
  • 24
  • I added the reference operator in front and it compiles with my code. So my new line is std::vector attrvector{&attribs...}; Does this preserve the type within the any class while in the vector? – Anthony Matarazzo Sep 30 '19 at 19:08
  • @AnthonyMatarazzo then you end up with a pointer inside each `std::any`. – Quentin Oct 01 '19 at 09:02
  • OK, I will reveal more of the code to show how It is being used, – Anthony Matarazzo Oct 02 '19 at 17:47
  • When I use objects, it does not compile. Within the system, attributes are c++ objects. for example, indexBy{"mainArea"}, objectTop{10_pct}, objectLeft{10_pct}, objectWidth{90_pct}, objectHeight{90_pct}, textColor{50, 50, 50}, background{100, 200, 200}, textFace{"FiraMono-Regular"}, textSize{20_pt}, textWeight{400}. – Anthony Matarazzo Oct 03 '19 at 15:13
  • I do not want to store a pointer, but rather copy the object into the std:any object with the type saved. – Anthony Matarazzo Oct 07 '19 at 17:58
  • Then try ```template std::vector createElement(const ATTRS... attribs) { std::vector attrvector{std::move(attribs...)}; return attrvector; }```? (code unchecked) – jwimberley Oct 07 '19 at 18:31