B
blaz.bratanic
Would it be possible to rewrite the code below, to avoid explicit template argument specification?
#include <iostream>
struct A {
enum {
pass_by_reference = true
};
A() { std::cout << "A constructor." << std::endl; }
A(A const& other) { std::cout << "A copy constructor." << std::endl; }
void print() { std::cout << "Print A" << std::endl; }
};
struct B {
enum {
pass_by_reference = false
};
B() { std::cout << "B constructor." << std::endl; }
B(B const& other) { std::cout << "B copy constructor." << std::endl; }
void print() { std::cout << "Print B" << std::endl; }
};
template <bool B, class T> struct cond {
typedef T type;
};
template <class T> struct cond<true, T> {
typedef T& type;
};
template <typename T>
void foo(typename cond<T:ass_by_reference, T>::type a) {
a.print();
}
int main() {
A a;
B b;
foo<A>(a);
foo<B>(b);
}
#include <iostream>
struct A {
enum {
pass_by_reference = true
};
A() { std::cout << "A constructor." << std::endl; }
A(A const& other) { std::cout << "A copy constructor." << std::endl; }
void print() { std::cout << "Print A" << std::endl; }
};
struct B {
enum {
pass_by_reference = false
};
B() { std::cout << "B constructor." << std::endl; }
B(B const& other) { std::cout << "B copy constructor." << std::endl; }
void print() { std::cout << "Print B" << std::endl; }
};
template <bool B, class T> struct cond {
typedef T type;
};
template <class T> struct cond<true, T> {
typedef T& type;
};
template <typename T>
void foo(typename cond<T:ass_by_reference, T>::type a) {
a.print();
}
int main() {
A a;
B b;
foo<A>(a);
foo<B>(b);
}