problems of template

compiler : visual c++ 2010
os : windows 7 64bits

learning template and encounter some problem

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
template<typename T>
void easyCopyImpl(T const &A, typename boost::enable_if_c< boost::is_array<T>::value>::type *temp = 0)
{
	std::cout<<"this is array"<<std::endl;
	std::cout<<typeid(T).name()<<std::endl;
	std::copy(A, A + sizeof_array(A), typename std::ostream_iterator<T>(std::cout, "") );
}

template<typename T>
void easyCopyImpl(T const &A, typename boost::enable_if_c< boost::is_class<T>::value>::type *temp = 0)
{
	std::cout<<"this is class"<<std::endl;
	//std::copy(A.begin(), A.end(), std::ostream_iterator<T>(std::cout, "") );
}

template<typename T>
void easyCopy(T const &A)
{
	easyCopyImpl(A);
}

int main()
{
  int A[] = {1, 2, 3, 4};
  easyCopy(A);
}


It would pop out a lot of error messages
What is the problem and how could I solve it?Thanks
Last edited on
Hello,
My guess is that the two easyCopyImpl templated methods have the same method signature (same name + same arguments type).
Try having a different method name and it should work.
I'm not sure how enable_if_c works but I suspect it does name a different type for each of the functions. I would like to see the error message.
To the best of my ability (which I admit is limited) I have taken your code and tried to make it compile on my system. Here is the code that includes the header files...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <boost/utility/enable_if.hpp>
#include <boost/type_traits/is_array.hpp>
#include <boost/type_traits/is_class.hpp>
#include <iostream>
#include <iterator>

template<typename T>
void easyCopyImpl(T const &A, typename boost::enable_if_c< boost::is_array<T>::value>::type *temp = 0)
{
	std::cout<<"this is array"<<std::endl;
	std::cout<<typeid(T).name()<<std::endl;
	std::copy(A, A + sizeof_array(A), typename std::ostream_iterator<T>(std::cout, "") );
}

template<typename T>
void easyCopyImpl(T const &A, typename boost::enable_if_c< boost::is_class<T>::value>::type *temp = 0)
{
	std::cout<<"this is class"<<std::endl;
	//std::copy(A.begin(), A.end(), std::ostream_iterator<T>(std::cout, "") );
}

template<typename T>
void easyCopy(T const &A)
{
	easyCopyImpl(A);
}

int main()
{
  int A[] = {1, 2, 3, 4};
  easyCopy(A);
}


The error I get is about sizeof_array not being found. Is that the error you're getting?

Just for grins, I replaced it with sizeof(), but get a problem about binary '=': no operator found which takes a right-hand operand of type 'const int', which is caused by line 12. If I comment out line 12, it compiles (but of course doesn't do much).

Please let us know what errors you are receiving, and we'll go from there.
Last edited on
The error I get is about sizeof_array not being found. Is that the error you're getting?

no, that is not the problem
I am sorry I forgot to release the codes of sizeof_array

1
2
template<typename T, size_t size>
inline size_t const sizeof_array(T const (&)[size]){return size;}


the problem is because the compiler would deduce the type T as "int [4]" rather than "int"
so I simply have to remove "[4]"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
template<typename T>
typename boost::enable_if< boost::is_array<T> >::type copy_out_impl(T const &A)
{
	std::cout<<"this is array : ";
	typename typedef boost::remove_extent<T>::type ValueType;	
	std::copy(A, A + sizeof_array(A), std::ostream_iterator<ValueType>(std::cout, "") );
	std::cout<<std::endl;
}

template<typename T>
typename boost::enable_if< boost::is_class<T> >::type copy_out_impl(T const &A)
{
	std::cout<<"this is class : ";
	typedef typename T::value_type ValueType;
	std::copy(A.begin(), A.end(), std::ostream_iterator<ValueType>(std::cout, "") );
	std::cout<<std::endl;
}

template<typename T>
void copy_out(T const &A)
{
	copy_out_impl(A);
}


Thanks for your help
Last edited on
Topic archived. No new replies allowed.