我想使用boost属性树write_json序列化,这样可以节省一切为字符串,这并不是说数据是错误的,但我需要每次都明确地投下他们,我想别的地方使用它们.(比如在python或其他C++ json(非boost)库中)
这里是一些示例代码以及我根据区域设置得到的内容:
boost::property_tree::ptree root, arr, elem1, elem2;
elem1.put<int>("key0", 0);
elem1.put<bool>("key1", true);
elem2.put<float>("key2", 2.2f);
elem2.put<double>("key3", 3.3);
arr.push_back( std::make_pair("", elem1) );
arr.push_back( std::make_pair("", elem2) );
root.put_child("path1.path2", arr);
std::stringstream ss;
write_json(ss, root);
std::string my_string_to_send_somewhare_else = ss.str();
Run Code Online (Sandbox Code Playgroud)
并且my_string_to_send_somewhere_else是某事.像这样:
{
"path1" :
{
"path2" :
[
{
"key0" : "0",
"key1" : "true"
},
{
"key2" : "2.2",
"key3" : "3.3"
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
反正是将它们保存为值,例如:
"key1" : true或"key2" : 2.2?
这个问题也适用于boost::function和std::tr1::function.
std::function 不相等的平等:
#include <functional>
void foo() { }
int main() {
std::function<void()> f(foo), g(foo);
bool are_equal(f == g); // Error: f and g are not equality comparable
}
Run Code Online (Sandbox Code Playgroud)
在C++ 11中,operator==和operator!=重载并不存在.在早期的C++ 11草案中,使用注释(N3092§20.8.14.2)将重载声明为已删除:
// deleted overloads close possible hole in the type system
Run Code Online (Sandbox Code Playgroud)
它没有说明"类型系统中可能存在的漏洞"是什么.在TR1和Boost中,声明了重载但未定义.TR1规范评论(N1836§3.7.2.6):
这些成员函数应保持未定义.
[ 注意:类似布尔值的转换会打开一个漏洞,通过
==或可以比较两个函数实例!=.这些未定义的void运算符会关闭漏洞并确保编译时错误.- 尾注 ]
我对"漏洞"的理解是,如果我们有bool转换函数,那么转换可以用于相等比较(以及其他情况):
struct S {
operator bool() { return false; }
};
int main() { …Run Code Online (Sandbox Code Playgroud) 假设我有一个将两个值相加的函数.如果我对类型一无所知,那么我基本上必须写两次函数; 一次在实际返回值中,再次作为返回类型说明符:
template <typename A, typename B>
auto Add(const A& a, const B& b) ->std::decay<decltype(a + b)>::type
{
return a + b;
}
Run Code Online (Sandbox Code Playgroud)
虽然这有效,但它是不可取的,因为它难以阅读且难以维护.
在C++ 14中,这不会是一个问题,因为我们可以删除返回类型说明符(我不确定它会不会衰减...).现在,我坚持使用C++ 11.
根据我的经验,无论何时我在C++中寻找尚未进入标准的功能,但有明显的需求,Boost库通常都有一个解决方案.我搜索了文档,但是我找不到任何可能对我有帮助的东西.这些BOOST_AUTO_RETURN和BOOST_TYPEOF_TPL功能似乎更多旨在为C++ 03用户提供C++ 11功能.
基本上我所追求的是执行以下功能的东西:
template <typename A, typename B>
auto Add(const A& a, const B& b)
{
return a + b; // Deduce return type from this, like C++14 would
}
Run Code Online (Sandbox Code Playgroud)
Boost库中是否有一些我不知道的功能(或C++ 11中的一个漂亮的技巧)可能允许我-> decltype(...)在每次自动返回类型后放弃显式?这将如何实施?
我有一个ini文件,其中包含一些示例值,如:
[Section1]
Value1 = 10
Value2 = a_text_string
Run Code Online (Sandbox Code Playgroud)
我正在尝试加载这些值并使用Boost在我的应用程序中打印它们,但我不明白如何在C++中执行此操作.
我在这个论坛中搜索,以便找到一些例子(我总是使用C,所以我在C++中不是很好)但我只找到了关于如何一次性从文件中读取值的示例.
我需要在我想要的时候只加载一个值,比如string = Section1.Value2因为我不需要读取所有的值,而只需要读取其中的一些值.
我想加载单个值并将它们存储在变量中,以便在我的应用程序中使用它们时使用它们.
使用Boost可以做到这一点吗?
目前,我正在使用此代码:
#include <iostream>
#include <string>
#include <set>
#include <sstream>
#include <exception>
#include <fstream>
#include <boost/config.hpp>
#include <boost/program_options/detail/config_file.hpp>
#include <boost/program_options/parsers.hpp>
namespace pod = boost::program_options::detail;
int main()
{
std::ifstream s("file.ini");
if(!s)
{
std::cerr<<"error"<<std::endl;
return 1;
}
std::set<std::string> options;
options.insert("Test.a");
options.insert("Test.b");
options.insert("Test.c");
for (boost::program_options::detail::config_file_iterator i(s, options), e ; i != e; ++i)
std::cout << i->value[0] << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
但这只是读取for循环中的所有值; 相反,我只想在需要时读取单个值,并且我不需要在文件中插入值,因为它已经用我在程序中需要的所有值写入.
我想迭代匹配类似的目录中的所有文件somefiles*.txt.
有boost::filesystem没有内置的东西,或者我需要一个正则表达式或每个东西leaf()?
我不太清楚这两个锁类之间的区别.据说在boost文档中,boost::unique_lock没有实现自动锁定.
这是否意味着之间的主要区别unique_lock,并lock_guard是用unique_lock我们必须显式调用lock()功能?
这更像是一个答案,而不是一个问题,因为我已经弄明白了,至少就干净地编译图书馆而言.对我来说,主要问题是让shared_ptr工作.
配料:
Boost v.1.45.0
STLport的版本位于http://www.anddev.org/viewtopic.php?p=29939.
NDK的版本r4b.
路线:
在你的Android.mk文件中添加:
LOCAL_CFLAGS += -DBOOST_EXCEPTION_DISABLE -D_STLP_NO_EXCEPTIONS -DOS_ANDROID -D_STLP_USE_SIMPLE_NODE_ALLOC
Run Code Online (Sandbox Code Playgroud)
在stlport/stl/_string.h的第613行删除对__stl_throw_length_error的调用.如果您愿意,可以使用_STLP_NO_EXCEPTIONS.
在第261行之后编辑boost/boost/smart_ptr/shared_ptr.hpp以消除对shared_ptr构造函数中对boost :: throw_exception的调用.我在方法的整个主体周围使用了#ifndef BOOST_EXCEPTION_DISABLE.(但请参阅下面的答案.)
接下来你需要提供一些缺失的部分.使用以下内容创建头文件:
#ifdef OS_ANDROID
#include <exception>
namespace std
{
struct bad_alloc : public exception { bad_alloc operator()(){}};
}
#endif
Run Code Online (Sandbox Code Playgroud)
和一个带有精简异常类的源文件,以支持bad_alloc:
#ifdef OS_ANDROID
#include <exception>
namespace std
{
exception::exception() {}
exception::~exception() {}
const char* exception::what() const {}
}
#endif
Run Code Online (Sandbox Code Playgroud)
在包含boost/shared_ptr.hpp的任何地方都包含标题.编译源代码并将其添加到库中.
我理解如何使用weak_ptr和shared_ptr.我shared_ptr通过计算其对象中的引用数来理解它是如何工作的.weak_ptr工作怎么样?我尝试阅读boost源代码,并且我不熟悉boost以了解它使用的所有内容.
谢谢.
尝试在我的计算机上编译Boost库时,我得到"未知的编译器版本 - 请运行配置测试并报告结果".
我有最近的Boost(截至发布日期) - 1.58.0.
不支持MSVC 14.0,但是?我如何"运行配置测试"?
.