template<typename OutputType> inline optional<OutputType> ValueComposer<OutputType>::compose(InternalStateContainer* internalState, const std::vector<std::string>* names, ValueComposerUserFunc<OutputType> getter) { //printf("ValueComposer compose 000-----.\n"); EStorageMap states; for (auto name : *names) { eframework::TypeID type = internalState->getType(name); if (type == TypeID::get<int>() && (internalState->get<int>(name)) != nullopt) { states.set<int>(name, *internalState->get<int>(name)); //printf("ValueComposer compose set---int--.\n"); } else if (type == TypeID::get<float>() && (internalState->get<float>(name)) != nullopt) { states.set<float>(name, *internalState->get<float>(name)); //printf("ValueComposer compose set---float--.\n"); } else if (type == TypeID::get<bool>() && (internalState->get<bool>(name)) != nullopt) { states.set<bool>(name, *internalState->get<bool>(name)); //printf("ValueComposer compose set---bool--.\n"); } else if (type == TypeID::get<std::string>() && (internalState->get<std::string>(name)) != nullopt) { states.set<std::string>(name, *internalState->get<std::string>(name)); //printf("ValueComposer compose set---string--.\n"); } else { //printf("ValueComposer compose set---nullopt--.\n"); return nullopt; } } //printf("ValueComposer compose 111-----.\n"); return getter(&states); }
时间: 2024-04-18 21:32:31 浏览: 169
这是一个ValueComposer类的成员函数模板`compose`的实现。该函数接受三个参数:一个指向`InternalStateContainer`对象的指针`internalState`,一个指向字符串向量的指针`names`,以及一个`ValueComposerUserFunc<OutputType>`类型的函数指针`getter`。
函数首先创建一个名为`states`的`EStorageMap`对象,用于存储状态值。然后遍历`names`向量中的每个字符串,通过调用`internalState`对象的`getType`函数获取相应名称的类型,并根据类型将对应的值存储到`states`对象中。
如果某个名称对应的类型不匹配或者值为`nullopt`,则返回空的`optional<OutputType>`。
最后,调用传入的`getter`函数指针,将`states`对象作为参数传递,并返回其结果。
注释部分是一些调试打印语句,可以用于调试程序时打印相关信息。
相关问题
template < typename _T, typename std::enable_if< std::disjunction<std::is_integral<_T>, std::is_enum<_T>>::value, int>::type = 0>
您的第一个问题是关于模板元编程的内容。您想要了解的是这段代码的作用是什么?
这段代码是一个模板函数的声明,使用了模板参数 `_T`。通过使用 `std::enable_if` 和 `std::disjunction`,它限制了 `_T` 只能是整型或枚举类型。同时,通过指定一个默认的整型模板参数(`int::type = 0`),它还提供了一个条件,以便在编译时进行选择性的启用或禁用函数模板。
换句话说,这段代码的作用是限制模板函数只能接受整型或枚举类型的参数,并且提供了一个条件,以便在编译时进行选择性的启用或禁用函数模板。
template <typename T> int compare<std::vector<T>>(const std::vector<T> &lhs, const std::vector<T> &rhs);有什么错误
在模板函数的函数名中,应该使用模板参数名 `T`,而不是具体的类型 `std::vector<T>`。因此,正确的写法应该是:
```
template <typename T>
int compare(const std::vector<T> &lhs, const std::vector<T> &rhs);
```
或者使用 `typename` 的简化形式:
```
template <class T>
int compare(const std::vector<T> &lhs, const std::vector<T> &rhs);
```
这样定义的模板函数可以比较两个 vector 容器内的元素是否相等。
阅读全文
相关推荐
















