强制模板静态成员实例化 [英] Force template static member instantiation

查看:219
本文介绍了强制模板静态成员实例化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建仅在模板实例化时才执行某些代码的程序(它将用于低级驱动程序初始化). 现在,我有以下解决方案.

I'm trying to create the program that executes some code only if the template is instantiated (it will be used for low-level driver initialization). Now I have the following solution.

class Initializer
{
public:
    Initializer(){
        // This code is executed once
    }

    void silly() const{

    }
};

template <class T>
class Proxy{
protected:
    static const Initializer init;
};

template<class T>
const Initializer Proxy<T>::init;

template<class T>
class MyTemplate : public Proxy<void>{
public:
    static void myMethod1(){
        init.silly();

        // ... Something useful
    }

    static void myMethod2(){
        init.silly();

        // ... Something useful
    }
};

仅当我在某处调用myMethod1()myMethod2()时,才会执行Initializer默认构造函数.

The Initializer default constructor is executed only in case I call myMethod1() or myMethod2() somewhere.

但是有办法摆脱这些init.silly();行吗?

But is there a way to get rid of those init.silly(); lines?

推荐答案

模板和低级驱动程序初始化?..我尝试使其尽可能为C :)以确保确切的行为.

template and low-level driver initialization?.. I'd try to make it as C as possible :) to ensure exact behavior.

您也许可以执行以下操作:

You can do something like this perhaps:

class Initializer
{
public:
    Initializer() {
        // This code is executed once
    }
};

template <class T>
class Proxy {
protected:
    Proxy()
    {
        static Initializer init;
    }
};

template<class T>
class MyTemplate : public Proxy<void> {
public:
    void myMethod1() {
        // ... Something useful
    }

    void myMethod2() {
        // ... Something useful
    }
};

您的所有代码仅使用静态函数,并没有真正表明您为什么要使用类和模板.通过我的更改,我将myMethod1myMethod2设置为非静态,并且Proxy()构造函数将创建一次Initializer.

All your code uses only static functions and doesn't really show why you would use classes and templates. With my change I made myMethod1 and myMethod2 non static and Proxy() constructor would create Initializer once.

请注意,由于所有这些模板混乱,您的Initializer可能会与实例化代理模板一样执行多次.你是真的吗如果不是,请转换为没有此意外结果的清晰可读代码.这对于其他人也将更好地维护和阅读:

Note that because of all that template mess your Initializer might be executed as many times as you instantiate Proxy template. Did you really mean it? If not, convert to clear readable code that doesn't have this unexpected results. This will also be better maintainable and readable for others:

class Initializer
{
    Initializer() {
        // This code is executed once
    }
public:
    void init()
    {
        static Initializer init;
    }
};


template<class T>
class MyTemplate {
public:
    static void myMethod1() {
        Initializer::init();
        // ... Something useful
    }

    static void myMethod2() {
        Initializer::init();
        // ... Something useful
    }
};

这绝对清楚地表明,Initializer将仅在调用myMethod1myMethod2之前创建一次.如果没有任何内容调用您的Initializer::init,则应在链接时删除Initializer中的代码.

This makes it absolutely clear that Initializer will be created only once just before myMethod1 or myMethod2 is called. If nothing calls your Initializer::init then that code from Initializer should be removed at link time.

这篇关于强制模板静态成员实例化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆