- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试转this C++ code/answer (创建 map string <-> some_array_of_predefined_function_types
的类)使用 boost/预处理器将其转换为更自动化的类。
我创建了以下代码:
#include <boost/preprocessor.hpp>
#include <iostream>
#include <string>
#include <map>
#include <vector>
#define CF_ADD_MAPS(z, n, T) \
std::map<std::string, identity< BOOST_PP_CAT(T, n ) >::type* > \
BOOST_PP_CAT(T , BOOST_PP_CAT(n , _var));
#define CF_ADD_OPERATORS(z, n, T) \
operator identity< BOOST_PP_CAT(T, n ) >::type*() \
{return \
BOOST_PP_CAT( p-> , BOOST_PP_CAT(T , BOOST_PP_CAT(n , BOOST_PP_CAT( _var, [n]) )) ) ; \
}
#define CF_ADD_INSERTERS(z, n, T) \
void insert(std::string name, identity< BOOST_PP_CAT(T, n ) >::type* ptr) \
{ \
BOOST_PP_CAT(T , BOOST_PP_CAT(n , BOOST_PP_CAT( _var, [name]) )) =ptr; \
}
template <typename T> struct identity { typedef T type; };
#define CF_DEFINE_MAP_STRUCT_I(name, number) \
template <BOOST_PP_ENUM_PARAMS(number, class T)> \
class name { \
BOOST_PP_REPEAT(number, CF_ADD_MAPS, T) \
friend class apitemp; \
public: \
class apitemp { \
std::string n; \
BOOST_PP_CAT(name, *) p; \
public: \
apitemp(std::string name_, BOOST_PP_CAT(name, *) parent) \
: n(name_), p(parent) {} \
BOOST_PP_REPEAT(number, CF_ADD_OPERATORS, T) \
}; \
BOOST_PP_REPEAT(number, CF_ADD_INSERTERS, T) \
apitemp operator[](std::string n) {return apitemp(n, this);} \
};
#define CF_MAP_CLASS_NAME my_map
#define CF_GENERATE_MAP_CLASS(z, n, data) CF_DEFINE_MAP_STRUCT_I(CF_MAP_CLASS_NAME, n)
#define CF_MAX_MAP_TYPES_COUNT 2
BOOST_PP_REPEAT(CF_MAX_MAP_TYPES_COUNT, CF_GENERATE_MAP_CLASS, x)
/*
class api {
//maps containing the different function pointers
std::map<std::string, identity<void()>::type* > voida;
std::map<std::string, identity<int(std::string, const int&)>::type* > stringcrint;
friend class apitemp;
public:
//api temp class
//given an api and a name, it converts to a function pointer
//depending on parameters used
class apitemp {
std::string n;
api* p;
public:
apitemp(std::string name, api* parent)
: n(name), p(parent) {}
operator identity<void()>::type*()
{return p->voida[n];}
operator identity<int(std::string, const int&)>::type*()
{return p->stringcrint[n];}
};
//insertion of new functions into appropriate maps
void insert(std::string name, identity<void()>::type* ptr)
{voida[name]=ptr;}
void insert(std::string name, identity<int(std::string, const int&)>::type* ptr)
{stringcrint[name]=ptr;}
//operator[] for the name gets halfway to the right function
apitemp operator[](std::string n) {return apitemp(n, this);}
} myMap;
*/
int hello_world(std::string name, const int & number )
{
name += "!";
std::cout << "Hello, " << name << std::endl;
return number;
}
int main() {
my_map<int(std::string, const int &)> myMap;
myMap.insert("my_method_hello", &hello_world );
int a = myMap["my_method_hello"]("Tim", 25);
std::cout << a << std::endl;
std::cin.get();
}
它不会编译并出现接下来的 5 个错误:
Error 1 error C2913: explicit specialization; 'my_map' is not a specialization of a class template
Error 2 error C2133: 'myMap' : unknown size
Error 3 error C2512: 'my_map' : no appropriate default constructor available
Error 4 error C2039: 'insert' : is not a member of 'my_map'
Error 5 error C2676: binary '[' : 'my_map' does not define this operator or a conversion to a type acceptable to the predefined operator
当预处理器生成时:
template <>
class my_map {
friend class apitemp;
public:
class apitemp {
std::string n;
my_map* p;
public: apitemp(std::string name_, my_map* parent) : n(name_), p(parent) {}
};
apitemp operator[](std::string n) {
return apitemp(n, this);
}
};
template < class T0>
class my_map {
std::map<std::string, identity< T0 >::type* > T0_var;
friend class apitemp;
public:
class apitemp {
std::string n;
my_map* p;
public:
apitemp(std::string name_, my_map* parent) : n(name_), p(parent) {}
operator identity< T0 >::type*() {
return p->T0_var[0] ;
}
};
void insert(std::string name, identity< T0 >::type* ptr) {
T0_var[name] =ptr;
}
apitemp operator[](std::string n) {
return apitemp(n, this);
}
};
我想知道我做错了什么以及如何让它编译?
最佳答案
template <>
class my_map {
// ...
};
问题出在 BOOST_PP_ENUM_PARAMS
上,这也将生成一个 0 参数版本。由于 0 参数版本 ( template<>
) 引入了特化,它会在你身上出错。 Fix:至少有一个固定参数。
下一个问题:您不能根据模板参数“重载”类。它们必须具有不同的名称或以其他方式构建,具有部分特化之类的东西。
关于c++ - Boost预处理器多个模板类生成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8374693/
我正在阅读哈德利的 Advanced R并尝试一些东西。我正在尝试创建一个 lazy闭包函数返回一个带有提供的函数 data.frame在其环境中以及使用 with并且能够在以后提供额外的函数参数。
我有两个 ViewController。初始 ViewController 是输入和存储 URL 的地方。此初始 ViewController 的 viewDidLoad 还应该在应用程序启动时开始加
你是怎么用的 对于应用程序中的 ListView 项也应该在设备 <11 上运行? 由于 activated_state 在 HC 之前不可用,我只能想到两个肮脏的解决方法: 在您的 Activit
我正在为 android (2.1 > 3.1) 编写一个应用程序,我想使用熟悉的做法,即在 Honeycomb 应用程序中使用应用程序图标来进入家庭 Activity ,但是,当我之前运行该 Act
如果搜索的键不存在,我如何覆盖方法 GET 或编写一个将在服务器端执行的新函数返回另一个键值? 示例: 如果关键字“word_1 word_2 word_3 word_4”不存在则搜索关键字“word
对于我的存储库,我使用的是 Git 和 Stash。在 Stash 端,我限制了(只读)对 master 的访问权限,因此任何用户都可以从 master 分支分支以获取功能/分支,但不能直接 merg
如何配置dgrid及其存储以定义渲染行时是否已经选择了行? 例如,如果我的行数据是这样的: { id: 1, name: 'Item Name', selected: true } 我当前
有没有一种方法可以将变量从一个 BeanShell 前/后处理器引用到另一个 BeanShell 处理器(它们在同一个线程组中)? 如果我在 HTTP 请求下的 BeanShell 预处理器中创建了一
问题 我已尝试添加预操作 shell 脚本,这些脚本会根据我正在构建的内容打开/关闭我的 .pch 文件中的某些定义。 但是,在运行构建时,没有任何反应。我不是一个流利的 shell 脚本编写者,所以
我有一个 HTML 字符串用作 jQuery 输入文档。 // the variable html contains the HTML code jQuery( html ).find( 'p' ).
在 Mercurial 中允许 merge 之前有没有办法进行一些检查? 通过将以下内容添加到 ~/.hg/hgrc,我找到了更新前 Hook ,并拥有一个在允许更新之前运行的脚本: [hooks]
总结: 预 Controller Hook 是否在缓存期间执行?是否有任何 Hook 点可以执行? (系统前?) 我应该强调一个事实,即 Hook 不会影响发送到浏览器的内容。这不是问题。 详细版:
我正在使用适用于 android 的 Skobbler Map API,到目前为止它一直非常好。按照官方的“操作方法”,我已经能够将 map 应用到我的应用程序中。比我可以让应用程序下载 map 并离
当我安装bcrypt时我的 hapi js 项目的模块尚未安装,它显示类似 node-pre-gyp install --fallback-to-build 我尝试通过运行来安装; npm i nod
我试图使用此代码的变体: apply plugin: 'java' apply plugin: 'idea' idea.workspace.iws.withXml { provider ->
假设我们有一个 PHP 项目,其依赖项 A 和 B 分别依赖于 PHP 库 X,但版本不同。 通常,人们会使用诸如 composer 之类的 PHP 依赖管理器,它可以通过在与 A 和 B 兼容的版本
这似乎违背了代码块的目的,但我希望能够在代码块中加粗。例如,如果我想将返回行加粗: int main(void) { **return 0;** } 最佳答案 您必须在 HTML 中执行此操作
我们是否应该使用 Huggingface(预)训练一个 BERT 无框模型的小写输入数据?我查看了 Thomas Wolf ( https://github.com/huggingface/trans
我有两个模式: 技能: var mongoose = require("mongoose"); var SkillSchema = new mongoose.Schema({ skill: {
我这里有问题。这适用于 Chrome,但我无法在 IE11 的 index.html 中使用任何动画。当它不想工作时,我会看到一个静态屏幕。同样在 IE 中,消息不会像它应该的那样消失。如果我将 di
我是一名优秀的程序员,十分优秀!