-1

I was reading on template deduction then tried to create one. as follows:-

#include <iostream>
#include <array>
using namespace std;
template<int i,typename A,typename... B> struct deducted{

};
//Deduction guide
template<typename A,typename... B> deducted(A a,B... b)->deducted<0,A,B...>;
//

int main(){
    deducted a={"","",""};
    deducted b={0,0,0};
    array c={0,0,0};
}

what supprises me is the test fails with error: too many initializers for 'deducted<0, const char*, const char*, const char*>' but the std::array version works fine. What could i be possibly missing, and can anyone rewrite much better?

D. Sikilai
  • 467
  • 1
  • 3
  • 17

2 Answers2

1

Deduction works, but then, construction fails.

Provide constructor. (or appropriate members for aggregate initialization).

Jarod42
  • 203,559
  • 14
  • 181
  • 302
1

You have too many initializers because there's noting to receive what you initialize it with.

Example fix:

#include <type_traits>

template<int i, typename A, typename... B>
struct deducted{
    A a;
    std::common_type_t<B...> arr[sizeof...(B)];
};

Demo

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108