Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++17 <functional> template parameter deductions not working on Xcode 10.1

Tags:

c++

xcode

I've been playing around with Template Argument Deduction with C++17 (and onwards), and was trying to compile this exact sample from cppreference.com

#include <functional>

int func(double) { return 0; }
int main() {
  std::function f{func}; // guide #1 deduces function<int(double)>
  int i = 5;
  std::function g = [&](double) { return i; }; // guide #2 deduces function<int(double)>
}

It compiles and runs fine in their web based compiler they have on that page, but when I try and compile this on my Macbook Pro, it fails, saying

error: no viable constructor or deduction guide for deduction of template arguments of 'function'

I've tried it both in an Xcode (v10.1) project set to C++17, and just running clang directly with -std=c++17.

I've also compiled the above example on different online compilers, such as here: https://godbolt.org/z/ERliha

I've also verified that type deductions for std::pair work, so I can't tell if:

  1. I screwed up my toolchains somehow when I was messing with cross compilers.
  2. Deduction Guides for this class are missing in Apple's Toolchain.
  3. I'm running an old toolchain (I don't know how to check this)
  4. ... (Any other reason)
like image 898
David Zech Avatar asked Nov 08 '18 23:11

David Zech


1 Answers

It looks like libc++ does not fully support all the deduction guides yet, if we look at the libc++ status page it say the proposal that brought this specific deduction guide is in progress:

P0433R2| LWG Toward a resolution of US7 and US14: Integrating template deduction for class templates into the standard library |Kona| In progress

We see from this godbolt session clang fails when using -stdlib=libc++ but not when using -stdlib=libstdc++.

The commits to libc++ for deduction guides don't indicate any commits for std::function.

We now have a bug report for this Bug 39606: std::function does not have deduction guides.

like image 135
Shafik Yaghmour Avatar answered Nov 18 '22 01:11

Shafik Yaghmour