Search code examples
c++g++compile-timeerror-checking

C++ how to prevent invoking a method more than one in Compile-time?


I have a method and I want to force the user calling it at last one in compile-time. Currently I have a correct implementation, But it is useful for runtime execution.

Is there a clean way to checking this problem (just one time calling) in compile-time?

static void set_logging_type(LOG_TYPE type)
{
    static bool select_type_done{false};

    if (!select_type_done)
    {
        log_type = type;
        select_type_done = true;
        return;
    }
    else
    {
        throw std::runtime_error("logging type is selected before!");
    }
}

Thanks in advance.


Solution

  • Is there a clean way to checking this problem (just one time calling) in compile-time?

    No.

    Determining how many times (or whether at all) set_logging_type() is called is equivalent to the halting problem, and thus undecidable (in general).