Monday, October 28, 2013

Template argument deduction

Newsgroup: comp.lang.c++

Subject: Template argument deduction

From: blaz.bratanic@...

Date: Mon, 28 Oct 2013 15:18:19 -0700 (PDT)



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::pass_by_reference, T>::type a) {

a.print();

}



int main() {

A a;

B b;



foo<A>(a);

foo<B>(b);

}







via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://www.pocketbinaries.com/usenet-forums/showthread.php?117177-Template-argument-deduction&goto=newpost

View all the progranning help forums at:

http://www.pocketbinaries.com/usenet-forums/forumdisplay.php?128-Coding-forums

No comments:

Post a Comment