Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

SFINAE

49 views
Skip to first unread message

Daniel

unread,
Jul 28, 2016, 5:31:27 PM7/28/16
to
I want to have a class template that has a specialization for bool, and also for std::vector<bool>::const_reference if that type differs from bool.

I came up with this:

template<class T, class Enable = void>
class A
{
public:
A()
{
std::cout << "default" << std::endl;
}
};


template <class T>
class A<T,typename std::enable_if<std::is_same<T,bool>::value>::type>
{
public:
A()
{
std::cout << "bool" << std::endl;
}

};

template<class T>
class A<T,typename std::enable_if<std::is_same<T,
std::conditional<!std::is_same<bool,std::vector<bool>::const_reference>::value,
std::vector<bool>::const_reference,
void>::type>::value>::type>
{
public:
A()
{
std::cout << "std::vector<bool>::const_reference" << std::endl;
}
};

// With VS, the types are the same
A<std::vector<bool>::const_reference> a1; // With VS, prints bool
A<bool> a2; // With VS, prints bool

Is there a simpler way?

Thanks,
Daniel

Öö Tiib

unread,
Jul 28, 2016, 7:01:08 PM7/28/16
to
On Friday, 29 July 2016 00:31:27 UTC+3, Daniel wrote:
> I want to have a class template that has a specialization for bool, and
> also for std::vector<bool>::const_reference if that type differs from bool.

That is odd ... AFAIK it is *required* to be bool.

>
> I came up with this:

...

>
> Is there a simpler way?

Wasn't it simple enough?
May be if you write that, then it is simpler to follow logically:

template<class T>
class A< T, typename std::enable_if<
std::is_same< std::vector<bool>::const_reference, T>::value
&& !std::is_same<bool, T>::value >::type >

Daniel

unread,
Jul 28, 2016, 7:16:36 PM7/28/16
to
On Thursday, July 28, 2016 at 7:01:08 PM UTC-4, Öö Tiib wrote:
> On Friday, 29 July 2016 00:31:27 UTC+3, Daniel wrote:
> > I want to have a class template that has a specialization for bool, and
> > also for std::vector<bool>::const_reference if that type differs from bool.
>
> That is odd ... AFAIK it is *required* to be bool.
>
I was under that impression too, but based on cppreference, which is
perhaps not the best source. Does anyone know for sure?

Anyway, it's been reported to me that with

Xcode, Apple LLVM 7.1 with -std=c++14 and libc++
(LLVM C++ standard library with C++11 support)

it seems not to be the case.

Daniel

Öö Tiib

unread,
Jul 28, 2016, 8:04:26 PM7/28/16
to
You are correct clang version 3.8.0 with libc++:
http://coliru.stacked-crooked.com/a/d4dfc706d72fc3bd

The libc++ architect Howard Hinnant explains why is that in answer to
same question:
http://stackoverflow.com/questions/31974237/why-is-libcs-vectorboolconst-reference-not-bool

I myself think that 'std::vector<bool>' was mistake.
I prefer 'std::bitset' instead of 'std::array<bool, N>' and
'boost::dynamic_bitset' instead of 'std::vector<bool>'.

Daniel

unread,
Jul 28, 2016, 10:17:04 PM7/28/16
to
On Thursday, July 28, 2016 at 8:04:26 PM UTC-4, Öö Tiib wrote:
>
> You are correct clang version 3.8.0 with libc++:
> http://coliru.stacked-crooked.com/a/d4dfc706d72fc3bd
>
> The libc++ architect Howard Hinnant explains why is that in answer to
> same question:
> http://stackoverflow.com/questions/31974237/why-is-libcs-vectorboolconst-reference-not-bool

Thanks for looking into that!
>
> I myself think that 'std::vector<bool>' was mistake.
> I prefer 'std::bitset' instead of 'std::array<bool, N>' and
> 'boost::dynamic_bitset' instead of 'std::vector<bool>'.

Agreed.

Daniel

0 new messages