0

I was reading the book C++ templates - the complete guide, 2nd edition and got the code from that which looks like this:-

template<typename T>
void showVal(const T &arg1, const T &arg2)
{
    std::cout << arg1 << arg2;
}

int main() {
    showVal("hello", "world1");
    return 0;
}

The above code gave me this error:- "error C2782: 'void showVal(const T &,const T &)': template parameter 'T' is ambiguous". This is reasonable because the arguments I am passing are deduced to const char[6] and const char[7]. To fix this, I have made the changes in the function which look like this after the change :-

template<typename T, std::size_t L1, std::size_t L2>
void showVal(const T (&arg1)[L1], const T(& arg2)[L2])
{
    std::cout << arg1 << arg2;
}

PS:- I've got this fix from the book

The main confusion underlies in the value of L1 and L2. How compiler knows that it has to pass 6 and 7 to the template parameter L1 and L2. Is there any rule for array type.

5
  • as you know, "hello" is deduced to be const char[6], so during the template deduction, the compiler knows that L1 has to be 6, since the first argument expects a reference to an array of a type T (deduced to const char) of a length L1 Commented Jul 30, 2018 at 12:24
  • 1
    i dont understand the question as you already give the answer yourself (even before the question) Commented Jul 30, 2018 at 12:37
  • Hi @user463035818,The question is simple. How compiler deduces the size even-though we never have sent the size while calling showVal. Is there any rule defined in c++ that can explain this. Commented Jul 30, 2018 at 12:38
  • ok sorry, sometimes its about tiny details. So you know that compiler deduces const char[6] and const char[7] as the types, but you want to know how that works? Commented Jul 30, 2018 at 13:09
  • Hi @user463035818, It's okay. Anyway, the comment made by user2079303 gave the answer. If you have anything you want to add that will be great. Thank you. Commented Jul 30, 2018 at 13:18

2 Answers 2

3

The type of "hello" is const char[6], as shown in the error message of your first attempt. The length of the array is 6. As you can see, the length of the array is part of the type of the array. Since the compiler has to know the type, it implicitly also knows the length.

Just like the template type argument T was deduced to be const char based on the type of the expressions passed to the non-template arguments arg1 and arg2 (those types being const char[6] and const char[7]), so too the template non-type arguments L1 and L2 were deduced from those same parameter types.

Sign up to request clarification or add additional context in comments.

Comments

1

function template

template<typename T, std::size_t L1, std::size_t L2>
void showVal(const T (&arg1)[L1], const T(& arg2)[L2])

for call showVal("hello", "world1") compiler will make implicit instantiation

showVal(const char (&arg1)[6], const T(& arg2)[7])

and that is the way you can prevent that array decays to pointer, binding reference to array type, you achieve that array size of arguments is known to function definition. so for call showVal("hello", "world1"), compiler deduced T is char and arguments type are array of char size 6 and 7 respectively.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.