Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++17 class template partial deduction

My understanding about the Template argument deduction for class templates proposal was to homogenize the behaviour of template functions and template classes in deduction contexts. But I think that I have misunderstood something.

If we have this template object:

template <std::size_t S, typename T> struct test {     static constexpr auto size = S;     using type_t = T;      test(type_t (&input)[size]) : data(input) {}     type_t (&data)[size]{}; }; 

I tend to use a helper function as syntactic sugar for creating test objects:

template <std::size_t S, typename T> test<S, T> helper(T (&input)[S]) { return input; } 

Which can be used as shown below:

int main() {     int buffer[5];      auto a = helper<5, int>(buffer); // No deduction     auto b = helper<5>(buffer);      // Type deduced     auto c = helper(buffer);         // Type and size deduced      std::cout << a.size << b.size << c.size;      return 0; } 

The code above outputs 555 as expected. I've tried the same in Wandbox using the newer compiler setup1:

int main() {     int buffer[5];      test<5, int> a(buffer); // No deduction: Ok.     test<5> b(buffer);      // Type deduced: FAILS.     test c(buffer);         // Type and size deduced: Ok.      std::cout << a.size << b.size << c.size;      return 0; } 

It looks like template argument deduction for class templates works only deducing all the parameters, I was expecting both behaviours (helper function and class template) to be the same, did I misunderstood something?


1The last compilers availables in Wandbox are gcc HEAD 7.0.1 201701 and clang HEAD 5.0.0 (trunk).

like image 202
PaperBirdMaster Avatar asked Jan 24 '17 16:01

PaperBirdMaster


People also ask

What is template argument deduction?

Template argument deduction is used when selecting user-defined conversion function template arguments. A is the type that is required as the result of the conversion. P is the return type of the conversion function template.

Can constructors be templated?

As long as you are satisfied with automatic type inference, you can use a template constructor (of a non-template class). @updogliu: Absolutely.

What is CTAD C++?

Class Template Argument Deduction (CTAD) is a C++17 Core Language feature that reduces code verbosity. C++17's Standard Library also supports CTAD, so after upgrading your toolset, you can take advantage of this new feature when using STL types like std::pair and std::vector.

How do you call a function template in C++?

Defining a Function TemplateA function template starts with the keyword template followed by template parameter(s) inside <> which is followed by the function definition. In the above code, T is a template argument that accepts different data types ( int , float , etc.), and typename is a keyword.


1 Answers

From this excellent trip report by Botond Ballo:

The feature as originally proposed included a provision for partial deduction, where you explicitly specify some of the template arguments, and leave the rest to be deduced, but this was pulled over concerns that it can be very confusing in some cases:

// Would have deduced tuple<int, string, float>, // but tuple<int> is a well-formed type in and of itself! tuple<int> t(42, "waldo", 2.0f); 
like image 69
metalfox Avatar answered Sep 30 '22 09:09

metalfox