如何获取当前保留的变量类型,并定义该类型的新变量(How to get currently held variant type, and define new variables of that type)
我有一个boost ::类型变体,例如:
typedef boost::variant< uint8_t, int8_t, uint16_t, int16_t, uint32_t, int32_t, float, double, std::string > StorageTt;
StorageTt
变量(例如val
在我的代码中稍后设置为这些存储类型之一。 我想检索val
当前持有的类型来定义更多相同类型的变量。 所以如果val
目前是uint16_t
,我想做类似的事情:typedef decltype(typeid(val)) StorageTt; StorageTt new_val = 32.0; // new_val should be a uint16_t
但这会产生一个const type_info类型。 我知道我能做到:
switch (val.which()) { case 0: // uint8_t case 1: //...
但我宁愿避免长期陈述,因为我必须多次这样做。
I have a boost::variant of types such as:
typedef boost::variant< uint8_t, int8_t, uint16_t, int16_t, uint32_t, int32_t, float, double, std::string > StorageTt;
A
StorageTt
variable, sayval
, is set to one of these storage types later in my code. I'd like to retrieve the type thatval
currently holds to define more variables of that same type. So ifval
is currently auint16_t
, I want to do something like:typedef decltype(typeid(val)) StorageTt; StorageTt new_val = 42; // new_val should be a uint16_t
but this yields a
const type_info
type. I know I can do:switch (val.which()) { case 0: // uint8_t case 1: //...
but I'd rather avoid the long switch-statement, because I have to do this multiple times.
原文:https://stackoverflow.com/questions/25914975
最满意答案
没有什么特别的,用Restler来做。
如果是静态方法,直接调用
Team::method(parameter)
否则创建一个实例
- 在构造函数中,如果您在许多方法中需要它并将其存储在私有变量中
- 在方法层面
如果您使用的是数据库模型,它可能已经为您提供了团队实例作为关系
There is nothing special, doing it with Restler.
If it is a static method directly call
Team::method(parameter)
Otherwise create an instance either
- at constructor if you need it in many methods and store it in a private variable
- at the method level
If you are using a database model, it may already provide you with an instance of team as a relationship
相关问答
更多-
将MySQL.php文件保存在vendor/DB/PDO文件夹中。 然后你不必编辑AutoLoader.php Keep the MySQL.php file in vendor/DB/PDO folder. Then you dont have to edit AutoLoader.php
-
您的评论不是有效的PHPDoc评论,它只是一个常规评论 请查看以下内容以获取正确的语法 dbc = DB_Connector_MySQL::getConnection(); $this->response = new stdClass(); ...
-
男孩,我觉得自己很蠢。 事实证明我实际上并不需要$ accessControlFunction。 我只需要在我的Test.php类中使用{@Requires ...}而不是{@requires ...}。 继续,好人! Boy, do I feel stupid. Turns out I don't actually need the $accessControlFunction. I just had to use {@Requires ...} instead of {@requires ...} in ...
-
创建默认类和默认操作以将其映射为根 在index.php (网关)上 require_once '../../restler/restler.php'; require_once 'say.php'; $r = new Restler(); //blank string as the second param removes class_name from the URL $r->addAPIClass('Say',''); $r->handle(); 这使得Say作为默认类,映射到你的API类的根Nex ...
-
task_list_id和assigned_id目前没有有效的phpdoc注释。 他们也没有任何分配的价值。 这使得它们成为api调用所需的参数。 但是你有{@required title, display_order} ,它会用无效参数覆盖所需的列表,从而不需要它们 task_list_id and assigned_id currently do not have valid phpdoc comments. They do not have any assigned value as well. Th ...
-
我们刚刚发布了一个更新来修复这个严格的标准警告 那应该为你解决 We have just just released an update to fix this strict standards warning That should fix it for you
-
解决了这个问题: location /api { if (!-f $request_filename) { rewrite ^(.*)$ /api/index.php last; } if (!-d $request_filename) { rewrite ^(.*)$ /api/index.php last; } } Solved with this: location /api { if (!-f $request_filena ...
-
让我试着找一个更简单的解决方案 c; Scope::register('Kiosk\\Drone\\Drone', function () ...
-
你必须启用apache mod_rewrite you have to enable apache mod_rewrite
-
没有什么特别的,用Restler来做。 如果是静态方法,直接调用Team::method(parameter) 否则创建一个实例 在构造函数中,如果您在许多方法中需要它并将其存储在私有变量中 在方法层面 如果您使用的是数据库模型,它可能已经为您提供了团队实例作为关系 There is nothing special, doing it with Restler. If it is a static method directly call Team::method(parameter) Otherwise ...