我看到这些术语在编程中遍布各地,我对它们的含义有一个模糊的概念.搜索显示,事实上,这些事情已被问到堆栈溢出.据我所知,语言中的静态/动态类型与强/弱类型略有不同,但这种差异是我的意思.不同的来源似乎使用不同的含义甚至可以互换地使用这些术语.我找不到谈论两者的地方,实际上说明了差异.如果有人能够在这里为我和世界其他地方清楚地说明这一点,那会更好.
terminology static-typing weak-typing strong-typing dynamic-typing
如何迭代元组(使用C++ 11)?我尝试了以下方法:
for(int i=0; i<std::tuple_size<T...>::value; ++i)
std::get<i>(my_tuple).do_sth();
Run Code Online (Sandbox Code Playgroud)
但这不起作用:
错误1:抱歉,未实现:无法将"Listener ..."扩展为固定长度的参数列表.
错误2:我不能出现在常量表达式中.
那么,我如何正确迭代元组的元素?
以下文章是我为模板参数包找到的第一个提案.
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1603.pdf
在第16页,它讨论了引入两个新的运算符[]和<>来访问参数包元素和参数包类型.
The suggested syntax for such an operator involves two new operators: .[] to access values and .<> to access types. For instance:
template<int N, typename Tuple> struct tuple_element;
template<int N, ... Elements>
struct tuple_element<tuple<Elements...> >
{
typedef Elements.<N> type;
};
template<int N, ... Elements>
Elements.<N>& get(tuple<Elements...>& t)
{ return t.[N]; }
template<int N, ... Elements>
const Elements.<N>& get(const tuple<Elements...>& t)
{ return t.[N]; }
Run Code Online (Sandbox Code Playgroud)
那么这些运营商在哪里?如果没有,他们的替代品是什么?
我已经使用C++几年了,但我还没有找到解决我经常遇到的问题的方法.知道如何解决它会很棒.
我现在所拥有的是:
// Client code:
switch(currentEnumValue)
{
case MyEnum::kValue01:
processData<MyEnum::kValue01>(data);
break;
case MyEnum::kValue02:
processData<MyEnum::kValue02>(data);
break;
default:
LOG("Invalid command");
break;
}
// Declarations
enum class MyEnum {kValue01, kValue02};
class MyClass
{
// code
template <MyEnum> void processData(char*); /* Implemented somewhere else */
}
template <> void MyClass::processData<MyEnum::kValue01>(char* data); /* Implemented somewhere else */
MyClass <> void MyClass::processData<MyEnum::kValue02>(char* data); /* Implemented somewhere else */
Run Code Online (Sandbox Code Playgroud)
我想删除开关因为很多原因.而不是我需要的东西,如:processData<runtime-decltype(currentEnumValue)>(data);
我知道关于typeid以及没有将编译时间和运行时混合在一起......但尽管如此,我还是希望找到一些解决方案,最好不包括宏.
我想通过索引获得可变参数模板中的类型.索引被指定为模板参数.我设法找到一个有效的'hack',但我相信它不符合可变参数模板编程的精神.此外,它使用额外的内存.
这是代码和一些解释:
template <typename... InputPortTypes>
class PipelineReceiver
{
protected:
// This tuple is used for storing types only
// Hence, I would like to get rid of it, but I am not sure how.
std::tuple<
std::function<std::unique_ptr<InputPortTypes> (int)>...
> InputPortsTuple;
// This vector is used for storing the actual objects
// This is needed to be able to access/change its elements
// during run time later on.
// The vector is used for storage of function pointers (i.e. of type std::function) …Run Code Online (Sandbox Code Playgroud)