Is there a way to prevent the compiler from putting a constexpr function into the object file?

Viewed 99

I have a constexpr function that I don't want to show up in the binary. Only the result should be in there, computed at compile time. Is this possible?

Edit: The question came up when I looked at the output of this piece of code and discovered that the binary didn't change in size, whether or not I made the variable constexpr in main. Compiled using mingw g++ -std=c++17 -O2

#include <iostream>
#include <tuple>

template <std::size_t I>
constexpr auto fizzbuzz_elem()
{
    if constexpr (I % 5 == 0 && I % 3 == 0) {
        return "FizzBuzz";
    } else if constexpr (I % 5 == 0) {
        return "Buzz";
    } else if constexpr (I % 3 == 0){
        return "Fizz";
    } else {
        return I;
    }
}

template <std::size_t...Is>
constexpr auto fizzbuzz_impl(std::index_sequence<Is...>){
    return std::make_tuple(fizzbuzz_elem<1 + Is>()...);
}

template <std::size_t N>
constexpr auto fizzbuzz(){
    return fizzbuzz_impl(std::make_index_sequence<N>());
}

int main() {
    auto res = fizzbuzz<42>();
    std::apply([](auto... e){ ((std::cout << e << std::endl), ...); }, res);
}
0 Answers
Related