使用通过模板注入的模板化类型

时间:2018-09-27 06:15:06

标签: c++ template-meta-programming generic-programming

这是我第一次尝试应用无疑难以理解的概念。我创建了一个通用的记录器类型,可以在编译时确定日志级别是否足够高以进行输出。这是一个有关编译器资源管理器的问题的示例:https://godbolt.org/z/2u4HhB。这是记录器:

static const int debug = 1;
static const int info = 2;
static const int warn = 3;
static const int error = 4;
static const int fatal = 5;

template<int level, bool should_log = false>
struct logger_function {
    static void log(std::string message) {
        // Do nothing in the default case.
    }
};

template<int level>
struct logger_function<level, true> {
    static void log(std::string message) {
        std::cerr << "[" << level << "] : " << message << std::endl;
    }
};

template<int minLevel>
struct std_err_logger {
    template<int levelValue>
    struct level {
        static constexpr bool shouldLogResult = levelValue >= minLevel;

        typedef logger_function<levelValue, shouldLogResult> log_function;

        static void log(std::string message) {
            log_function::log(message);
        }
    };
};

,它的用法如下:std_err_logger<debug>::level<info>::log("Message.");

到目前为止,效果很好。当我尝试通过另一个模板注入记录器类型时,问题就开始了-毕竟我以后可能更喜欢记录到文件中。

 template<typename logger>
    class log_client {
        log_client() {
            logger::level<fatal>::log("Worked!");
        }
    };

并将类型注入客户端:

int main() {
    log_client<std_err_logger<debug>> log();
    return 0;
}

但是强大的g ++编译器并不高兴:

src/alsa/alsa_frame_recorder.h: In constructor ‘alsa::log_client<logger>::log_client()’:
src/alsa/alsa_frame_recorder.h:21:31: error: ‘::log’ has not been declared
             logger::level<1>::log("Worked!");
                               ^~~
src/alsa/alsa_frame_recorder.h:21:31: note: suggested alternative: ‘long’
             logger::level<1>::log("Worked!");
                               ^~~
                               long

1 个答案:

答案 0 :(得分:4)

问题是您使用了不正确的从属模板。您应该写:

template<typename logger>
class log_client {
    log_client() {
        logger::template level<fatal>::log("Worked!");
    }
};

有关为什么需要此处template的说明,请参阅this question