What is the extension in Visual Studio 2017 Disambiguates "bool" versus "std :: function" when passing in lambda?

The following compilations in Visual Studio 2017 with the MSVC compiler, but do not compile in GCC or Clang.

#include <iostream>
#include <functional>

void functionTest(std::function<void()>) {
    std::cout << "F\n";
}

void functionTest(bool) {
    std::cout << "B\n";
}

int main() {
    functionTest([](){ std::cout << "wut"; });
}

To fix this, we can use enable_f like this:

#include <iostream>
#include <functional>

void functionTest(std::function<void()>) {
    std::cout << "F\n";
}

template<typename BOOL_TYPE, typename = typename std::enable_if<std::is_same<bool, BOOL_TYPE>::value>::type>
void functionTest(BOOL_TYPE) {
    std::cout << "B\n";
}

int main() {
    functionTest([](){ std::cout << "wut"; });
}

Or I can disambiguate by entering the user type instead of bool (this is what you will need to do in the case of a constructor that has an ambiguity problem):

#include <iostream>
#include <functional>

void functionTest(std::function<void()>) {
    std::cout << "F\n";
}

enum class DescriptiveTypeName {False, True};
void functionTest(DescriptiveTypeName) {
    std::cout << "B\n";
}

int main() {
    functionTest([](){ std::cout << "wut"; });
}

The problem I am encountering here is that I have a non-trivial game project and am trying to compile Xcode for iOS. As far as I can tell, I can't get the same behavior as Visual Studio for all compilers (this would be nice). Therefore, as a result, I try to change my project so that it becomes more consistent with the standard.

Visual Studio, , , , . Xcode, , .

, - Visual Studio .

+4
2

- > bool - > pointer- > bool. " ", .

MSVC -, . , .

- > - > bool ( ), MSVC - . .

, , .

, , SFINAE . :

template<class T>
struct exactly {
  T t;
  template<class U, std::enable_if_t<std::is_same<T, std::decay_t<U>>{}, int> =0>
  exactly( U&& u ):t(std::forward<U>(u)) {}
  exactly():t() {}
  exactly(exactly&&)=default;
  exactly(exactly const&)=default;

  operator T() const& { return t; }
  operator T() && { return std::move(t); }
  T& get()& { return t; }
  T const& get() const& { return t; }
  T get()&& { return std::move(t); }
};

:

void functionTest(exactly<bool> b) {
  std::cout << "B\n";
}

.

fancy SFINAE , SFINAE.

+4

:

  • MSVC lambda - . bool MSVC.

  • , , , MSVC , , , bool . function .

bool , , -, , .

+7

Source: https://habr.com/ru/post/1683306/


All Articles