I am trying to get automatic template argument deduction to work in C++. I have written…
Private research annotation
Sign in to add a private research label or note.
- User
I am trying to get automatic template argument deduction to work in C++. I have written a function which accepts a std::map with any two arbitrary types as its input, and returns a variable with the first type as its output. The following example works and compiles successfully:
codestd::map<std::string, int> map1; map1["apple"] = 1; string key1 = getkey(map1, 1);The following does not:
codestd::map<int, std::string> map2; map2[1] = "apple"; int key2 = getkey(map2, "apple");The compiler says it cannot deduce the types used by the map. What is the cause of this?
- Assistant
The cause of this is that template argument deduction for functions does not work when the arguments are in different positions in the parameter list for the two function calls. In the first example, the key type is std::string and the value type is int, so the template argument deduction can correctly determine that T is std::string and U is int. In the second example, the key type is int and the value type is std::string, so the deduction fails because the types are in a different order.
To fix this, you can explicitly specify the template arguments for the function call:
c++int key2 = getkey<int, std::string>(map2, "apple");Alternatively, you can rearrange the order of the template parameters in the function definition to match the order of the types in the map:
c++template<typename T, typename U> T getkey(const std::map<T, U>& m, const U& value) { // ... }With this definition, both calls to the function will work without explicit template argument specification.
- User
So, for clarity, am I correct to assume that the root of the problem is that the compiler won't know that "apple" is an std::string without being provided some kind of a hint?
Like in the angle brackets in your first example, or that the second input parameter will have the same typename as the map value in your second example?
- Assistant
Yes, in C++ simply writing "apple" will give a string literal.
In your first example the compiler knows to convert this argument into an std::string, because that is the type that your map1 expects. Whereas in your second example your templated getkey function receives the two types, std::string from the type of map2 and the string literal from the parameter you gave, and is unable to know which way to convert them without some kind of a hint.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →