By David Zech


2018-11-08 23:05:32 8 Comments

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)

1 comments

@Shafik Yaghmour 2018-11-09 00:46:27

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.

@David Zech 2018-11-09 00:53:42

Good find. It looks like Apple only has an old libstdc++ for Xcode projects, so I'm out of luck.

@Louis Dionne 2018-11-09 18:54:02

Xcode actually uses a fairly recent libc++, however libc++ (even the current trunk) does not support this. See bugs.llvm.org/show_bug.cgi?id=39606

Related Questions

Sponsored Content

1 Answered Questions

[SOLVED] Template argument deduction in function signature using guide

1 Answered Questions

2 Answered Questions

2 Answered Questions

[SOLVED] C++17 class template partial deduction

  • 2017-01-24 16:40:58
  • Paula_plus_plus
  • 899 View
  • 22 Score
  • 2 Answer
  • Tags:   c++ templates c++17

2 Answered Questions

[SOLVED] How to do the template recursion in function call?

  • 2017-08-01 07:06:35
  • Saumya Sambit Acharya
  • 83 View
  • 2 Score
  • 2 Answer
  • Tags:   c++ recursion

7 Answered Questions

[SOLVED] Use one argument for template parameter deduction?

0 Answered Questions

1 Answered Questions

1 Answered Questions

[SOLVED] Nested template parameters and type deduction

2 Answered Questions

Sponsored Content