我可以根据C ++中的运行时条件定义一个类型别名吗? [英] Can I define a type alias depending on runtime conditions in C++?

查看:174
本文介绍了我可以根据C ++中的运行时条件定义一个类型别名吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个OpenCL计划,该计划使用 cl_khr_byte_addressable_store 扩展以允许在内核代码中进行字节级写入;为了使我的程序也可以在不支持扩展的GPU上工作,我需要检查扩展是否可用,并使用 int 类型而不是<$ c $

I am working on an OpenCL Program which uses the "cl_khr_byte_addressable_store" extension to allow for byte level writes in Kernel code; in order for my program to also work on GPUs which do not support the extension, I need to check whether the extension is available and use an int type instead of char on platforms on which it is not.

在内核代码中,我可以简单地添加以下代码片段并使用 writeable_type 在整个我的内核代码;如果扩展可用,则预处理器定义 cl_khr_byte_addressable_store 将由OpenCL内核编译器自动添加。

In the kernel code, I can simply add the following snippet and use writeable_type throughout the my kernel code; the preprocessor definition cl_khr_byte_addressable_store is automatically added by the OpenCL Kernel compiler iff the extension is available.

/* kernel.cl */
#ifdef cl_khr_byte_addressable_store
    typedef char writeable_type;
#else
    typedef int writeable_type;
#endif

f() {
    writeable_type x = 1;
}

在我的主机代码中,我没有预处理器定义,我知道)检查扩展是否可用只能在运行时执行:

In my host code I don't have that preprocessor definition, and (as far as I know) the check whether extensions are available or not can only be done at run-time:

/* host.cpp */
void execute() {
    std::string extensions;
    this->opencl->getExtensions(extensions);
    if (extensions.find("cl_khr_byte_addressable_store") != std::string::npos) {
        // extension is available
        typedef cl_char writeable_type;
    }
    else {
        // extension is not available
        typedef cl_int writeable_type;
    }
    writeable_type x = 1;
}

因为typedef在编译时解析并且只在定义它们的块内有效,上面的例子不工作。

And that's my problem; because typedefs are resolved at compile time and only valid within the block in which they are defined, the example above does not work.

是否有可能以某种方式这么做?

Is it possible to somehow do this anyway?

感谢rlc,Neil Butterworth和talonmis让我走上正确的道路!我的问题的解决方案是相当简单:

Thanks rlc, Neil Butterworth and talonmis for putting me on the right path! The solution to my problem turns out to be fairly simple:

/*host.cpp*/

void execute() {
   bool extension_available = ...;  
   if (extension_available)
       this->_execute<cl_char>();
   else
       this->_execute<cl_int>();
}

template <typename writeable_char>
void _execute() {
    writeable_type x = 1;
    // ...
}


推荐答案

如果您在公共函数与类型不可知的类中拥有所有代码,则可以执行以下操作:

if you have all your code in a class of which the public functions are agnostic of the type, you can do something like this:

// assuming these are defined in OpenCL somewhere
typedef char cl_char;
typedef int cl_int;

// the public part of the base class cannot use the 
// writable size because it cannot know the size of the writable type
class Base
{
public :
    /* stuff */
    virtual void doStuff() = 0;
    /* more stuff */
};

// the writable type's size is known in this class and is
// either cl_char or cl_int, so this is where data that has
// to know the size will be carried around, and where API calls
// that need to know the exact writable type go.
// Code that is agnostic of the writable type need not be here
// (but can be)
template < typename WritableType >
class Derived : public Base
{
public :
    virtual void doStuff()
    {
        /* do stuff */
    }
};

int main()
{
    bool condition(false);
    /* magic to check for type size here */
    // once you get here, you know what the writable type is going to be,
    // so you can create an instance of the derived class accordingly
    Base *o;
    if (condition)
    {
        o = new Derived< cl_char >;
    }
    else
    {
        o = new Derived< cl_int >;
    }
    // as of here, you no longer need to know what the writable type
    // is because that knowledge is encapsulated in the object o
    o->doStuff();
}

模板类,它根据您在运行时需要的类型执行一切,并使用您需要的版本。

i.e. template the class that does everything according to the type you need at run-time, and use the version you need.

这篇关于我可以根据C ++中的运行时条件定义一个类型别名吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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