Skip to content

Instantly share code, notes, and snippets.

@ousttrue
Created February 22, 2014 06:07
Show Gist options
  • Save ousttrue/9149399 to your computer and use it in GitHub Desktop.
Save ousttrue/9149399 to your computer and use it in GitHub Desktop.
std::initializer_list test in vc2013
#include <iostream>
#include <limits>
#include <vector>
#include <string>
#include <list>
template<typename T>
struct node
{
T value;
// crash in vc2013
//std::list<node<T>> children;
std::vector<node<T>> children;
public:
node(const T &t)
: value(t)
{
//std::cout << __FUNCTION__ << std::endl;
}
node(const T &t, const std::initializer_list<node<T>> &init)
: value(t)
{
//std::cout << __FUNCTION__ << std::endl;
for (auto it = init.begin(); it != init.end(); ++it){
children.push_back(*it);
}
}
node(const node<T> &rhs)
{
//std::cout << __FUNCTION__ << std::endl;
*this = rhs;
}
node& operator=(const node<T> &rhs)
{
//std::cout << __FUNCTION__ << std::endl;
if (this != &rhs){
value = rhs.value;
children = rhs.children;
}
return *this;
}
node(const node<T> &&rhs)
{
//std::cout << __FUNCTION__ << std::endl;
*this = std::move(rhs);
}
node& operator=(const node<T> &&rhs)
{
//std::cout << __FUNCTION__ << "&&" << std::endl;
if (this != &rhs){
value = std::move(rhs.value);
children = std::move(rhs.children);
}
return *this;
}
~node()
{
//std::cout << __FUNCTION__ << std::endl;
}
};
template<typename T>
std::ostream &operator<<(std::ostream &os, const node<T> &t)
{
os << t.value;
if (!t.children.empty()){
os << "{";
for (auto it = t.children.begin(); it != t.children.end(); ++it)
{
if (it != t.children.begin()){
os << ",";
}
os << *it;
}
os << "}";
}
return os;
}
struct Joint
{
std::string name;
float pos[3];
Joint()
{
pos[0] = 0;
pos[1] = 0;
pos[2] = 0;
}
Joint(const std::string &_name, float x, float y, float z)
: name(_name)
{
pos[0] = x;
pos[1] = y;
pos[2] = z;
}
};
std::ostream &operator<<(std::ostream &os, const Joint &joint)
{
std::cout << "[" << joint.name << ":"
<< joint.pos[0]
<< "," << joint.pos[1]
<< "," << joint.pos[2]
<< "]"
;
return os;
}
int main(int argc, char **argv)
{
{
typedef node<int> tree;
auto t = tree(1, {
tree(2),
tree(3, {}),
tree(4, {
tree(5),
})
});
std::cout << t << std::endl;
}
{
typedef node<std::string> tree;
auto t = tree("a", {
tree("aa"),
tree("ab", {}),
tree("ac", {
tree("aca"),
})
});
std::cout << t << std::endl;
}
{
typedef node<Joint> tree;
auto t = tree(Joint("root", 0, 0, 0), {
tree(Joint("joint1", 1, 2, 3))
});
std::cout << t << std::endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment