Skip to content

Instantly share code, notes, and snippets.

View thiagomg's full-sized avatar

Thiago Massari Guedes thiagomg

View GitHub Profile
//Construtor padrão
TestCopy() {
cout << "[CONSTR 1]" << endl;
this->val = 0;
}
//Construtor parametrizado
TestCopy(T val) {
cout << "[CONSTR 2:" << val << "]" << endl;
this->val = val;
TestCopy(T val) {
cout << "[CONSTR 2:" << val << "]" << endl;
this->val = val;
}
//Saída do programa:
TestCopy<> t1; //[CONSTR 1]
TestCopy<> t2 {1}; //[CONSTR 2:1]
TestCopy<> t3 = t2; //[COPY]
TestCopy<> t4 {t2}; //[COPY]
TestCopy<> t5 = TestCopy<>{5}; //[CONSTR 2:5]
TestCopy<> t6 = {6}; //[CONSTR 2:6]
TestCopy<> t7 = 7; //[CONSTR 2:7]
t5.ts(); //5===============
t1 = t3; //[OP=1]
t1.ts(); //1===============
t1 = TestCopy<>{3}; //[CONSTR 2:3]
//[OP=3]
t3.ts(); //1===============
vector<TestCopy<>> tm;
tm.push_back(TestCopy<>(99)); //[CONSTR 2:99]
//[MOVE]
tm[0].ts(); //99===============
@thiagomg
thiagomg / macro_templ.cpp
Last active August 29, 2015 14:20
Do macro ao template
//Generico. Sem tipos declarados
#define FILL(v, i, start, end, step) { \
for(i=start; i < end; i+=step) \
v.push_back(i); \
}
#define ACCUM(v, ret) { \
for(size_t i=0; i < v.size(); i++) \
ret += v[i]; \
}
@thiagomg
thiagomg / macro_templ.cpp
Created May 5, 2015 01:09
Do macro ao template
template<typename T, typename U>
void fill(T &t, U start, U end, U step) {
for(U i=start; i < end; i += step) {
t.push_back(i);
}
}
template<typename T>
auto accum(T &t) {
typename T::value_type ret{};
@thiagomg
thiagomg / macro_templ.cpp
Last active August 29, 2015 14:20
Do macro ao template
template<typename T, typename U>
void fill2(T &v, U start, U step) {
*begin(v) = start;
generate(begin(v)+1, end(v), [&start, &step]() { return start += step; });
//alternative - this is not a proper std::fill, but a generate
//for(auto &item : v) {
// item = start;
// start += step;
//}
void log_print(const std::string &str) { /* ... */ }
bool isInfoEnabled() { /* ... */ }
#define LOG_INFO(fmt, x) { \
if(isInfoEnabled()) log_print(boost::str( boost::format(fmt) % x) ); \
}
#include <vector>
#include <cstdio>
using namespace std;
//this function will be generated
int c_accum_i(vector<int> &v)
{
int ret{};
for(const auto &i : v) {