First, I have read this very informative answer about stylistic difference between defining a constexpr variable vs constexpr function. My question is more related with the final size of the binary when using these two. Consider this code:
// approach 1
template <typename T>
struct foo
{
static constexpr char name[] = "mickey";
};
// approach 2
template <typename T>
struct bar
{
static constexpr const char* getName() { return "mickey"; }
};
const char* func1() { return foo<int>::name; }
const char* func2() { return foo<double>::name; }
const char* func3() { return bar<int>::getName(); }
const char* func4() { return bar<double>::getName(); }
Look at this code in this godbolt link as well. While approach 1 returns different copies of name, approach 2 returns only one copy for all the different instantiations for different T. Infact, when I created 100 different types approach 2 led to a considerably smaller binary. Was wondering if anyone has experienced something similar.