以下代码是否安全?
boost::any any_value;
{
std::string s = "HelloWorld";
any_value = s;
}
std::string ss = any_cast<std::string>(any_value);
Run Code Online (Sandbox Code Playgroud) 我希望摆脱对我的代码的boost依赖.我有以下结构构造.在代码中的另一个地方调用和使用此结构时boost::any_cast.我知道模板类会这样做,但发现很难写这个模板. - C++新秀.
struct Properties {
public:
Properties() {}
Properties(const std::string &s, const boost::any & p) {
name = s;
value = p;
}
template <typename T>
Properties(T n) {
value = n;
}
boost::any value;
std::string name;
};
Run Code Online (Sandbox Code Playgroud) 我想通过索引获得可变参数模板中的类型.索引被指定为模板参数.我设法找到一个有效的'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) 在尝试boost::any通过boost::any_cast引用转换检索实例后,我无法保持常量正确性。
我的代码:
MyMap paramMapToSet;
MyMap& paramMap = ¶mMapToSet;
const MyMap& constParamMap = ¶mMapToSet;
A hoe;
paramMap.set(hoe, "structA");
// this works
A& hoeRef = paramMap.getByRef<A>("structA");
hoeRef.myInt = 101;
cout << paramMap.get<A>("structA").myInt << endl; // prints 101
// as well as this:
hoe = constParamMap.get<A>("structA");
cout << hoe.myInt << endl;
// and this:
const A& constHoeRef = paramMap.getByRef<A>("structA");
cout << constHoeRef.myInt << endl;
// however this doesn't work, why?? (error message below)
const A& constHoeRef = constParamMap.getByRef<A>("structA");
cout …Run Code Online (Sandbox Code Playgroud) Boost <boost/any.hpp>有:
template<typename ValueType>
ValueType any_cast(any & operand);
template<typename ValueType>
inline ValueType any_cast(const any & operand);
Run Code Online (Sandbox Code Playgroud)
(以及其他变体.)这种组合不应该导致诸如boost::any_cast<int>(my_any);?之类的调用模糊不清吗?
我问,因为如果我写这个程序:
#include <boost/any.hpp>
#include <iostream>
template<typename ValueType>
ValueType any_cast(boost::any & operand)
{
return boost::any_cast<ValueType>(operand);
}
int main()
{
int x = 123;
boost::any my_any(x);
std::cout << "my_any = " << any_cast<int>(my_any) << "\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我确实抱怨模糊不清:
g++ -std=c++14 -O3 -Wall -pedantic -pthread main.cpp && ./a.out
main.cpp: In function 'int main()':
main.cpp:14:57: error: call of overloaded 'any_cast(boost::any&)' is …Run Code Online (Sandbox Code Playgroud) 我有一个很多boost :: any的向量在这个向量中我需要对std :: vector和IContainer类型的元素执行一些操作
class IContainer
{
public:
virtual ~IContainer(){}
virtual const boost::any operator[](std::string) const = 0;
};
class AContainer : public IContainer
{
std::vector<int> vect_;
std::string name_;
public:
AContainer() : vect_({0, 1, 2, 3, 4, 5}), name_("AContainer") {}
virtual const boost::any operator[](std::string key) const
{
if (key == "str")
return (name_);
if (key == "vect")
return (vect_);
return nullptr;
}
};
Run Code Online (Sandbox Code Playgroud)
所以我做了以下功能(非常丑陋),但谁工作正常
我是 const std::vector<boost::any>&
for (const auto & elem : m)
{
try
{
std::vector<int> v = …Run Code Online (Sandbox Code Playgroud)