TCD
TCD

Reputation: 163

Symbol table entry when using auto return type deduction with templates in C++

Does anyone know the reason why the output of nm -C on an executable that contained a templated function defined with auto return type deduction has the word auto in the symbol table?

Why does it need to know the return type was deduced? I had assumed that the compiler would just replace the auto with the actual type.

Below is an example followed by the output of nm -c

#include <iostream>

// Concrete function. Not called
float add(float a, float b){ 
        return a+b;
}

// Concrete function. auto return type deduction
auto add(int a, int b){ 
        return a+b;
}

// Templated function. One parameter
template <typename T>
auto add (T a, T b){ 
        std::cout << "One parameter\n";
        return a+b;
}

// Templated function. Two parameters
template <typename T, typename U>
auto add (T a, U b){ 
        std::cout << "Two parameters\n";
        return a+b;
}

int main()
{
        add(1,2);       // call concrete function
        add(3.0,4.0);   // generate and call one-parameter template function
        add(1,2.0);     // generate and call two-parameter template function    
    
        return 0;
}

And nm -C gives

001159 T add(float, float)
00123b W auto add<double>(double, double)
001173 T add(int, int)
00127c W auto add<int, double>(int, double)

Why does the compiler not instead generate

W double add<double>(double, double)

i.e. with the auto replaced by double for the templated function? That is what it will say if I replace the above by

template <typename T>
double add (T a, T b){ 
        std::cout << "One parameter\n";
        return a+b;
}

Upvotes: 1

Views: 61

Answers (0)

Related Questions