【发布时间】:2017-05-05 21:13:14
【问题描述】:
为我自己开发一个命令行解析器。我马上就知道我在使用这个结构时会遇到问题,并希望有人可以提供解决方法的建议。
我想将参数的参数列表(基于模板)存储在可能包含各种不同数据类型的vector 中。但据我了解,您必须静态定义vector<template<type>>。有没有办法排除多种类型?
这是我的意思的一个例子:
#include <vector>
#include <memory>
namespace clparser
{
class CommandLine {
private:
std::vector<Parameter<AnyType??>*> ArgumentList;
public:
void Add(Parameter<AnyType??>* Parameter) { ArgumentList.push_back(Parameter); }
};
template<typename T>
class Parameter {
private:
const char *ShortOption;
const char *LongOption;
const char *Description;
const bool RequiredField;
const char *DefaultValue;
public:
Parameter(const char *ShortOption, const char *LongOption, const char *Description, const bool RequiredField, const char *DefaultValue)
: ShortOption(ShortOption), LongOption(LongOption), Description(Description), RequiredField(RequiredField), DefaultValue(DefaultValue)
{
}
};
}
【问题讨论】:
-
可能是
boost::any? -
我宁愿没有任何 boost 依赖,但如果我不能用 STL 完成这个...谢谢
-
c++17 起
any也将在std中提供 -
太棒了。感谢您的快速响应!
-
如果你知道类型,考虑使用 std::variant