Compatibility issue with GCC/Clang code.

  • Thread starter Thread starter Ybaca
  • Start date Start date
Y

Ybaca

Guest
When trying to compile with VS17 C++ code that had I previously compiled using GCC, the compiler spits a whole bunch of errors.

My code makes heavy use of templates and SFINAE-based class specialization, and it seems that VC++ has difficulties with implicit type deduction even where GCC (and Clang) could manage.

What is going on? I am familiar with VS17 but it is the first time I try to use it for C++ so perhaps there is an option or something I need to set in the IDE?

Here is a fragment of code that compiles without problem with GCC and Clang but fails with VS17.

#include <iostream>
#include <stdlib.h>
#include <vector>

template<typename T>
using if_lvalue = typename std::enable_if<std::is_lvalue_reference<T>::value, std::nullptr_t>::type;

template<typename T>
using if_reverse_iterator = decltype(std::declval<T>().rbegin(), nullptr);

template<class C, typename valuetype = std::nullptr_t, typename X = std::nullptr_t>
class reverser;

template< class C >
class reverser<C, if_lvalue<C>, if_reverse_iterator<C>>
{
public:

protected:
C& collection;
typedef decltype(collection.rbegin()) iterator;
public:
iterator begin() { return collection.rbegin(); }
iterator end() { return collection.rend(); }
reverser(C & v) : collection(v) {}
};


template<class T>
reverser<T&> make_reverser(T& v) {
return reverser<T&>(v);
}


int main() {
std::vector<int> v = { 1,2,3,4,5,6 };
auto m1 = make_reverser(v);
for (auto i : m1) { std::cout << i << ", "; }
std::cout << std::endl;
}





Continue reading...
 
Back
Top