在11 C++之前我有这样的事情:
template<class T,class U,class V>
struct Foo : T,U,V {
bool init() {
if(!T::init() || !U::init() || !V::init())
return false;
// do local init and return true/false
}
};
Run Code Online (Sandbox Code Playgroud)
我想将其转换为C++ 11可变参数语法,以获得灵活长度参数列表的好处.我理解使用递归解压缩模板arg列表的概念,但我无法看到正确的语法.这是我尝试过的:
template<typename... Features>
struct Foo : Features... {
template<typename F,typename... G>
bool recinit(F& arg,G&& ...args) {
if(!F::init())
return false;
return recinit<F,G...>(args...);
}
bool init() {
// how to call recinit() from here?
}
};
Run Code Online (Sandbox Code Playgroud)
我更喜欢对基类init()函数的调用顺序是从左到右,但它并不重要.
我的情况就像这个人为的例子:
template<class TFeature> struct Controller {};
template<class TController,typename T> struct Feature {
typedef Feature<TController,T> FeatureType;
};
typedef Controller<Feature::FeatureType,int> DefaultController;
Run Code Online (Sandbox Code Playgroud)
Controller模板化接受功能,我的问题是某些功能需要控制器的类型作为模板参数.这使得样本最后一行的typedef无法编译.
这是可能的还是我需要重新考虑设计?
我有一个模板类,接受1到8个整数参数.每个参数的允许范围是0..15.每个参数的默认值16允许我检测未使用的参数.
我想将用户提供的参数数量作为编译时常量.我可以使用模板助手类和许多部分特化来完成此操作.
我的问题是,我可以使用一些递归元编程来清理它.我有什么作品,但感觉它可以在语法上得到改善.
遗憾的是,我无法使用变量模板和其他任何c ++ 0x.
#include <stdint.h>
#include <iostream>
template<uint8_t p0,uint8_t p1,uint8_t p2,uint8_t p3,uint8_t p4,uint8_t p5,uint8_t p6,uint8_t p7>
struct Counter { enum { COUNT=8 }; };
template<uint8_t p0,uint8_t p1,uint8_t p2,uint8_t p3,uint8_t p4,uint8_t p5,uint8_t p6>
struct Counter<p0,p1,p2,p3,p4,p5,p6,16> { enum { COUNT=7 }; };
template<uint8_t p0,uint8_t p1,uint8_t p2,uint8_t p3,uint8_t p4,uint8_t p5>
struct Counter<p0,p1,p2,p3,p4,p5,16,16> { enum { COUNT=6 }; };
template<uint8_t p0,uint8_t p1,uint8_t p2,uint8_t p3,uint8_t p4>
struct Counter<p0,p1,p2,p3,p4,16,16,16> { enum { COUNT=5 }; };
template<uint8_t p0,uint8_t p1,uint8_t p2,uint8_t p3>
struct Counter<p0,p1,p2,p3,16,16,16,16> { …Run Code Online (Sandbox Code Playgroud) 我是Android开发的新手,我想实现Spinner.我的问题是我有一个数组,其中有一个关键类别的值为9.我的数组看起来像:
MyArray
[{Category=Things To Do},
{Category=Transportation},
{Category=Food and Drink},
{Category=Accommodation},
{Category=Shopping},
{Category=Money and Costs},
{Category=Business},
{Category=Turkey Tour},
{Category=Events}]
Run Code Online (Sandbox Code Playgroud)
我想获得类别键的价值.我需要MyArray for spinner如下:
MyArray
{
Things To Do
Transportation
Food and Drink
Accommodation
Shopping
......
}
Run Code Online (Sandbox Code Playgroud)
它类似于iPhone代码.
// iPhone code
for(int i=0; i<[arrayCategory count]; i++)
{
NSString *strSubTitle=[[arrayCategory objectAtIndex:i]objectForKey:@"category"];
}
Run Code Online (Sandbox Code Playgroud)
任何的想法?
请帮我.
谢谢