P(std::initializer_list<Tag> children) : Tag("p", std::move(children)) {}
clang tidy warns as follows.
error: std::move
of the variable 'children' of the trivially-copyable type std::initializer_list<Tag>
has no effect; remove std::move()
[hicpp-move-const-arg,performance-move-const-arg,-warnings-as-errors]
P(std::initializer_list<Tag> children) : Tag("p", std::move(children)) {}
Is the reason why std::initializer_list cannot move because it is an array?
I looked up the definition of trivially-copyable, but I'm not sure.
#include <iostream>
#include <string>
#include <vector>
namespace html {
struct Tag
{
std::string name;
std::string text;
std::vector<Tag> children;
std::vector<std::pair<std::string, std::string>> attributes;
friend std::ostream &operator<<(std::ostream &os, const Tag &tag)
{
os << "<" << tag.name;
for (const auto &att : tag.attributes) os << " " << att.first << "=\"" << att.second << "\"";
if (tag.children.size() == 0 && tag.text.length() == 0) {
os << "/>" << std::endl;
} else {
os << ">" << std::endl;
if (tag.text.length()) { os << tag.text << std::endl; }
for (const auto &child : tag.children) { os << child; }
os << "</" << tag.name << ">" << std::endl;
}
return os;
}
protected:
Tag(std::string name, std::string text) : name{ std::move(name) }, text{ std::move(text) } {}
Tag(std::string name, std::vector<Tag> children) : name{ std::move(name) }, children{ std::move(children) } {}
};
struct P : Tag
{
explicit P(std::string text) : Tag{ "p", std::move(text) } {}
P(std::initializer_list<Tag> children) : Tag("p", std::move(children)) {}
};
struct IMG : Tag
{
explicit IMG(std::string url) : Tag{ "img", "" } { attributes.emplace_back(make_pair("src", std::move(url))); }
};
}// namespace html
int main1()
{
using namespace html;
std::cout <<
P{ IMG{ "http://pokemon.com/pikachu.png" } }
<< std::endl;
getchar();
return 0;
}