Whats wrong with my std::enable_if?

Hi I want to overload output operator<< but I want to do it differently for integers and for floating point numbers so I tried to define it like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
template<bool B, typename T = void>
using Enable_if = typename std::enable_if<B, T>::type;

template<typename T>
Enable_if<std::is_floating_point<T>::value,std::ostream&> 
operator<<(std::ostream& os, const Bound_form<T>& bf)
{
        //stuff for floating points
}

template<typename T>
Enable_if<std::is_integral<T>::value, std::ostream&> 
operator<<(std::ostream& os, const Bound_form<T>& bf)
{
        //stuff for integers
}


Im getting link errors with these bad boys, however with this everything is working fine

1
2
3
4
std::ostream& operator<<(std::ostream& os, const Bound_form<double>& bf)
{
        //floating point stuff
}


Is there something wrong with my Enable_if?

These 2 function I created to check what exactly was wrong were working fine
1
2
3
4
5
6
7
8
9
10
11
template<typename T>
Enable_if<std::is_floating_point<T>::value, void> check()
{
	std::cout << "This is a floating point type!!!!\n";
}

template<typename T>
Enable_if<std::is_integral<T>::value, void> check()
{
	std::cout << "This is an integral type!!!!\n";
}
Last edited on
:> Im getting link errors
http://www.cplusplus.com/forum/general/113904/#msg622073


ah, sorry skipped the last part. ¿Can you provide a minimal example that does reproduce your issue?
Last edited on
Tnx a lot for reply man! Just come home and I was ready to post a bit more detailed situation here, started to delete unnecessary parts of my code and with fresh head I realized the problem.

These 2 functions
1
2
3
4
5
6
7
template<typename T>
Enable_if<std::is_floating_point<T>::value,std::ostream&> 
operator<<(std::ostream& os, const Bound_form<T>& bf);

template<typename T>
Enable_if<std::is_integral<T>::value, std::ostream&> 
operator<<(std::ostream& os, const Bound_form<T>& bf);


were declared as friends in one of my classes but this way

friend operator<<(std::ostream& os, const Bound_form<T>& bf);

When I updated declarations to full ones including enable_if part that everything worked fine :) But tnx a lot for showing interest. Much appreciated!

Here whats fixed it. I hope this is the right way
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
33
34
35
36
37
38
39
template<bool B, typename T = void>
using Enable_if = typename std::enable_if<B, T>::type;


template<typename T>class Form;

template<typename T>
struct Bound_form
{
	const Form<T>& f;
	T val;
};

template<typename T>
class Form
{
	template<typename U>
	friend Enable_if<std::is_floating_point<U>::value, std::ostream&>
		operator<<(std::ostream& os, const Bound_form<U>& bf);

	template<typename U>
	friend Enable_if<std::is_integral<U>::value, std::ostream&>
		operator<<(std::ostream& os, const Bound_form<U>& bf);
        //...
};

template<typename T>
Enable_if<std::is_floating_point<T>::value, std::ostream&>
operator<<(std::ostream& os, const Bound_form<T>& bf)
{
        //...
}

template<typename T>
Enable_if<std::is_integral<T>::value, std::ostream&>
operator<<(std::ostream& os, const Bound_form<T>& bf)
{
        //...
}
Last edited on
Topic archived. No new replies allowed.