//
// main.cpp
// TestConstness
//
// Created by Marco Antognini on 27/10/13.
// Copyright (c) 2013 Marco Antognini. All rights reserved.
//
#include <iostream>
#include <memory>
#include <vector>
class X {
int x;
public:
X(int y) : x(y) { }
void print() const { std::cout << "X::" << x << std::endl; }
void foo() { ++x; }
};
using XPtr = std::shared_ptr<X>;
using ConstXPtr = std::shared_ptr<X const>;
XPtr createX(int x) { return std::make_shared<X>(x); }
void goo(XPtr p) {
p->print();
p->foo();
p->print();
}
void hoo(ConstXPtr p) {
p->print();
// p->foo(); // ERROR :-)
}
using XsPtr = std::vector<XPtr>;
using ConstXsPtr = std::vector<ConstXPtr>;
void ioo(ConstXsPtr xs) {
for (auto p : xs) {
p->print();
// p->foo(); // ERROR :-)
}
}
ConstXsPtr toConst(XsPtr xs) {
ConstXsPtr cxs(xs.size());
std::copy(std::begin(xs), std::end(xs), std::begin(cxs));
return cxs;
}
void joo(XsPtr xs) {
for (auto p: xs) {
p->foo();
}
}
// ERROR :-)
//XsPtr toNonConst(ConstXsPtr cxs) {
// XsPtr xs(cxs.size());
// std::copy(std::begin(cxs), std::end(cxs), std::begin(xs));
// return xs;
//}
class E {
XsPtr xs;
public:
E() {
for (auto i : { 2, 3, 5, 7, 11, 13 }) {
xs.emplace_back(createX(std::move(i)));
}
}
void loo() {
std::cout << "\n\nloo()" << std::endl;
ioo(toConst(xs));
joo(xs);
ioo(toConst(xs));
}
void moo() const {
std::cout << "\n\nmoo()" << std::endl;
ioo(toConst(xs));
joo(xs); // Should not be allowed
ioo(toConst(xs));
}
};
int main(int argc, const char * argv[])
{
std::cout << "Hello, World!\n";
XPtr p = createX(42);
goo(p);
hoo(p);
E e;
e.loo();
e.moo();
// WORKS!
return 0;
}
ICAgIC8vCiAgICAvLyAgbWFpbi5jcHAKICAgIC8vICBUZXN0Q29uc3RuZXNzCiAgICAvLwogICAgLy8gIENyZWF0ZWQgYnkgTWFyY28gQW50b2duaW5pIG9uIDI3LzEwLzEzLgogICAgLy8gIENvcHly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ogICAgfQoKICAgIENvbnN0WHNQdHIgdG9Db25zdChYc1B0ciB4cykgewogICAgICAgIENvbnN0WHNQdHIgY3hzKHhzLnNpemUoKSk7CiAgICAgICAgc3RkOjpjb3B5KHN0ZDo6YmVnaW4oeHMpLCBzdGQ6OmVuZCh4cyksIHN0ZDo6YmVnaW4oY3hzKSk7CiAgICAgICAgcmV0dXJuIGN4czsKICAgIH0KCiAgICB2b2lkIGpvbyhYc1B0ciB4cykgewogICAgICAgIGZvciAoYXV0byBwOiB4cykgewogICAgICAgICAgICBwLT5mb28oKTsKICAgICAgICB9CiAgICB9CgogICAgLy8gRVJST1IgOi0pCiAgICAvL1hzUHRyIHRvTm9uQ29uc3QoQ29uc3RYc1B0ciBjeHMpIHsKICAgIC8vICAgIFhzUHRyIHhzKGN4cy5zaXplKCkpOwogICAgLy8gICAgc3RkOjpjb3B5KHN0ZDo6YmVnaW4oY3hzKSwgc3RkOjplbmQoY3hzKSwgc3RkOjpiZWdpbih4cykpOwogICAgLy8gICAgcmV0dXJuIHhzOwogICAgLy99CgogICAgY2xhc3MgRSB7CiAgICAgICAgWHNQdHIgeHM7CiAgICBwdWJsaWM6CiAgICAgICAgRSgpIHsKICAgICAgICAgICAgZm9yIChhdXRvIGkgOiB7IDIsIDMsIDUsIDcsIDExLCAxMyB9KSB7CiAgICAgICAgICAgICAgICB4cy5lbXBsYWNlX2JhY2soY3JlYXRlWChzdGQ6Om1vdmUoaSkpKTsKICAgICAgICAgICAgfQogICAgICAgIH0KCiAgICAgICAgdm9pZCBsb28oKSB7CiAgICAgICAgICAgIHN0ZDo6Y291dCA8PCAiXG5cbmxvbygpIiA8PCBzdGQ6OmVuZGw7CiAgICAgICAgICAgIGlvbyh0b0NvbnN0KHhzKSk7CgogICAgICAgICAgICBqb28oeHMpOwoKICAgICAgICAgICAgaW9vKHRvQ29uc3QoeHMpKTsKICAgICAgICB9CgogICAgICAgIHZvaWQgbW9vKCkgY29uc3QgewogICAgICAgICAgICBzdGQ6OmNvdXQgPDwgIlxuXG5tb28oKSIgPDwgc3RkOjplbmRsOwogICAgICAgICAgICBpb28odG9Db25zdCh4cykpOwoKICAgICAgICAgICAgam9vKHhzKTsgLy8gU2hvdWxkIG5vdCBiZSBhbGxvd2VkCgogICAgICAgICAgICBpb28odG9Db25zdCh4cykpOwogICAgICAgIH0KICAgIH07CgogICAgaW50IG1haW4oaW50IGFyZ2MsIGNvbnN0IGNoYXIgKiBhcmd2W10pCiAgICB7CiAgICAgICAgc3RkOjpjb3V0IDw8ICJIZWxsbywgV29ybGQhXG4iOwoKICAgICAgICBYUHRyIHAgPSBjcmVhdGVYKDQyKTsKCiAgICAgICAgZ29vKHApOwogICAgICAgIGhvbyhwKTsKCiAgICAgICAgRSBlOwogICAgICAgIGUubG9vKCk7CiAgICAgICAgZS5tb28oKTsKCiAgICAgICAgLy8gV09SS1MhCgogICAgICAgIHJldHVybiAwOwogICAgfQoK