SFINAE with variation patterns?

I cannot find a suitable solution for using SFINAE with variation patterns.

Let's say I have a variable template object that does not like links:

template<typename... Args> class NoRef { //if any of Args... is a reference, this class will break //for example: std::tuple<std::unique_ptr<Args>...> uptrs; }; 

And a class that conveniently checks if the package of arguments contains links:

 template<typename T, typename... Other> struct RefCheck { static const bool value = std::is_reference<T>::value || RefCheck<Other...>::value; }; template<typename T> struct RefCheck<T> { static const bool value = std::is_reference<T>::value; }; 

How to use this to specialize NoRef for the case when links are present in the arg package?

+6
source share
2 answers

This does not use SFINAE, but basically does what you intend:

 template<bool Ref, typename... Args> class NoRef_; template<typename... Args> class NoRef_<false, Args...> { std::tuple<std::unique_ptr<Args>...> uptrs; }; template<typename... Args> class NoRef_<true, Args...> { // contains reference }; template<typename... Args> using NoRef = NoRef_<RefCheck<Args...>::value, Args...>; // alternative from Nawaz template<typename... Args> struct NoRef : NoRef_<RefCheck<Args...>::value, Args...> {} 
+9
source

I am afraid this is not possible because template packages are bulky. However, you can pack the packages.

 // Used to transport a full pack in a single template argument template <typename... Args> struct Pack {}; 

We adapt the verification check:

 template <typename T, typename... Other> struct RefCheck { static const bool value = std::is_reference<T>::value || RefCheck<Other...>::value; }; template <typename T> struct RefCheck<T> { static const bool value = std::is_reference<T>::value; }; template <typename... Args> struct RefCheck<Pack<Args...>>: RefCheck<Args...> {}; 

And now we can use Pack :

 template <typename P, bool = RefCheck<P>::value> class NoRef; template <typename... Args> class NoRef<Pack<Args...>, false> { // no reference in Args... here }; 
+6
source

All Articles