Is template bloated code still relevant with the latest compilers / linkers

I read that the latest versions of linkers filter out duplicate definitions in several translation units, there, referring to the problem of bloating code in relation to templates.

So, even if I use the compilation enable model, my code using templates should not inflate the code.

My request relates to the use of patterns (metaprogramming) as follows:

template <int N> int fact () 
{ 
    return fact<N-1>() * N; 
}

template <> int fact<1> () 
{ 
    return 1; 
}

int main()
{
    cout << fact<10>() << endl;
}

The exe size for the above code is approximately 8K. If I pass 100 instead of 10, the code size increases to 19K.

I'm mainly trying to understand coding patterns that can lead to code bloat when using patterns.

EDIT: after Yakk's comment, I recompiled using -O3, now the size is almost the same.

, ( )?

+4
1

, non-constexpr, . , , , , - , , .

, , , , . static const int :

    #include <iostream>

    template <int N>
    struct fact {
        static const int value = fact<N - 1>::value * N;
    };

    template <>
    struct fact<0> {
        static const int value = 1;
    };

    int main() {
        std::cout << fact<10>::value << std::endl;
    }

10 100 , , 100! 32- .

0

Source: https://habr.com/ru/post/1611837/


All Articles