相关疑难解决方法(0)

你能用C编写面向对象的代码吗?

你能用C编写面向对象的代码吗?特别是关于多态性.


另请参阅堆栈溢出问题C中的面向对象问题.

c oop object

481
推荐指数
19
解决办法
28万
查看次数

带有C++宏的可选参数

有没有办法用C++宏获取可选参数?某种超载也会很好.

c++ macros

97
推荐指数
9
解决办法
8万
查看次数

动态调度到模板函数 C++

我有一个模板函数(在我的例子中是一个 cuda 内核),其中有少量布尔模板参数可以在运行时选择。我很高兴在编译时实例化所有排列并动态调度,就像这样(对于布尔值 b0、b1、b2):

if (b0) {
    if (b1) {
        if (b2) {
            myFunc<true,true,true,otherArgs>(args);
        } else {
            myFunc<true,true,false,otherArgs>(args);
        }
    } else {
        if(b2) {
            myFunc<true,false,true,otherArgs>(args);
        } else {
            myFunc<true,false,false,otherArgs>(args);
        }
    }
} else {
    if(b1) {
        if(b2) {
            myFunc<false,true,true,otherArgs>(args);
        } else {
            myFunc<false,true,false,otherArgs>(args);
        }
    } else {
        if(b2) {
            myFunc<false,false,true,otherArgs>(args);
        } else {
            myFunc<false,false,false,otherArgs>(args);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这写起来很烦人,如果我最终得到 b3 和 b4,情况会变得更糟。

有没有一种简单的方法可以在 C++11/14 中以更简洁的方式重写它而不引入大型外部库(如 boost)?就像是:

const auto dispatcher = construct_dispatcher<bool, 3>(myFunc);
Run Code Online (Sandbox Code Playgroud)

...

dispatcher(b0,b1,b2,otherArgs,args);
Run Code Online (Sandbox Code Playgroud)

c++ templates c++11

5
推荐指数
1
解决办法
616
查看次数

标签 统计

c++ ×2

c ×1

c++11 ×1

macros ×1

object ×1

oop ×1

templates ×1