我正在使用一种变体为C ++中的语法分析器存储一系列类型。语法规则的每个组成部分都有一个类别(枚举类型)和一个值。成分根据类别存储值的类型。为了示例,我将类别简化为“字符串” =>存储字符串,而“数字” =>存储整数。
我想根据类别枚举获取具有正确类型的成分的值。我该怎么办?
我在下面编写了示例代码,其中构造了两个组成部分:strCon(存储字符串)和intCon(存储int),并尝试获取它们的值。
我想将strCon中的字符串分配给strVal, 并将int从intCon转换为intVal。
#include <variant>
struct Constituent
{
enum class Category {String, Number};
using Value = std::variant<std::string, int>;
Category cat;
Value val;
// Using a struct ideally to allow partial specialisation of the template,
// so I can pass the enum without the return type.
template<Category T>
struct OfCategory {};
template<Category T, typename U>
friend U const& getValue(OfCategory<T>, Constituent const&);
}
using Category = Constituent::Category;
// Template to return the value as the correct type
// for the constituent's category.
template<Category T, typename U>
U const& getValue(OfCategory<T> type, Constituent const& constituent)
{
// Uses the variant's get function.
return std::get<U>(constituent.val);
}
// Specialisation to return string from Category::String.
template<>
string const& getValue(OfCategory<Category::String> type,
Constituent const& constituent)
{
return getValue<Category::String, string>(constituent);
}
// Specialisation to return int from Category::Number.
template<>
int const& getValue(OfCategory<Category::Number> type,
Constituent const& constituent)
{
return getValue<Category::Number, int>(constituent);
}
int main()
{
Constituent strCon = {Category::String, "This is a string!"};
Constituent intCon = {Category::Number, 20};
// In my current implementation, I want this to work with
// the type wrapper as an overload for the function.
string strVal = getValue(OfCategory<Category::String>{}, strCon);
int intVal = getValue(OfCategory<Category::Number>{}, intCon);
// But it would be better to directly use the template.
strVal = getValue<Category::String>(strCon);
intVal = getValue<Category::Number>(intCon);
// The only way I can get it to work, is to explicitly provide
// the return type, which defeats the point.
strVal = getValue<Category::String, string>(
OfCategory<Category::String>{}, strCon);
intVal = getValue<Category::Number, int>(
OfCategory<Category::Number>{}, intCon);
// Ideally, I could use the cat parameter in Constituent to dynamically
// infer the return type, but I don't believe something like this is
// possible in C++.
}
答案 0 :(得分:2)
您可以通过创建一个中间特征类来进行间接定向:
enum E
{
X,
Y
};
template <E e>
struct Traits;
template <>
struct Traits<X>
{
using type = std::string;
};
template <>
struct Traits<Y>
{
using type = int;
};
template <E e>
typename Traits<e>::type get();
template <>
typename Traits<X>::type get<X>()
{
return "";
}
template <>
// actually, using the appropriate type directly works as well...
int get<Y>()
{
return 7;
}
您现在可以调用以下函数:
std::string s = get<X>();
int n = get<Y>();
答案 1 :(得分:1)
您需要添加一些特征以提供枚举类型,例如重用OfCategory
:
template<Category T> struct OfCategory;
template<> struct OfCategory<Category::String> { using type = std::string; };
template<> struct OfCategory<Category::Number> { using type = int; };
然后,无需其他专门知识:
template <Category T>
const typename OfCategory<T>::type&
getValue(OfCategory<T> type, Constituent const& constituent)
{
// Uses the variant's get function.
return std::get<typename OfCategory<T>::type>(constituent.val);
}
如getValue(OfCategory<Category::String>{}, strCon)
这样的通话。
甚至:
template <Category T>
const typename OfCategory<T>::type&
getValue(Constituent const& constituent)
{
// Uses the variant's get function.
return std::get<typename OfCategory<T>::type>(constituent.val);
}
用于像getValue<Category::String>(strCon);
答案 2 :(得分:0)
我怀疑这样的事情会起作用:
template<Category T>
auto getValue(OfCategory<T> type, Constituent const& constituent)
-> decltype(std::get<T>(constituent.val))
{
return std::get<T>(constituent.val);
}
(可能需要将T
强制转换为size_t
)。换句话说,您的getValue
是std::get