boost interview questions
Top boost frequently asked interview questions
I am looking for a way to get the output of a command when it is run from within a C++ program. I have looked at using the system() function, but that will just execute a command. Here's an example of what I'm looking for:
std::string result = system( "./some_command" ) ;
I need to run an arbitrary command and get it's output. I've looked at Boost.org but I have not found anything that will give me what I need.
Source: (StackOverflow)
I am considering starting to use the Boost.Log logging library. Is anyone using Boost.Log? Please share your experiences in this regard.
The other libraries I am considering are Apache log4cxx (it seems tedious to install but my team mates want something simple to get started) and Pantheios (the same problem since it works with extra front and back end).
Source: (StackOverflow)
I ran across enable_shared_from_this
while reading the Boost.Asio examples and after reading the documentation I am still lost for how this should correctly be used. Can someone please give me an example and/or and explanation of when using this class makes sense.
Source: (StackOverflow)
So, I've been reading through and it appears that the Boost libraries get used a lot in practice (not at my shop, though). Why is this? and what makes it so wonderful?
Source: (StackOverflow)
What is the difference between the following set of pointer? When do you use each pointer in a production code, if at all?
Examples would be appreciated!
scoped_ptr
shared_ptr
weak_ptr
intrusive_ptr
Edit#1
Do you guys use boost in production code?
Source: (StackOverflow)
Motivation: reason why I'm considering it is that my genius project manager thinks that boost is another dependency and that it is horrible because "you depend on it"(I tried explaining the quality of boost, then gave up after some time :( ). Smaller reason why I would like to do it is that I would like to learn c++11 features, because people will start writing code in it.
So:
- Is there a 1:1 mapping between
#include<thread> #include<mutex>
and
boost equivalents?
- Would you consider a good idea to replace boost stuff with c++11
stuff. My usage is primitive, but are there examples when std doesnt
offer what boost does? Or (blasphemy) vice versa?
P.S.
I use GCC so headers are there.
Source: (StackOverflow)
The documentation available on the boost website is... limited.
From what I've been able to read, the general consensus is that it is simply difficult to find good documentation on the boost::asio library.
Is this really the case? If so, why?
Notes:
- I have already found the (non-boost) Asio website - and the documentation looks to be identical to that on the boost website.
- I know that Boost::asio is new! I'm looking for solutions not excuses.
Edit:
- There is a proposal to add a networking library to standard library for TR2 written by the author of Boost:asio (Christopher Kohlhoff). While it isn't documentation for boost:asio, it does use it as a base for the TR2 proposal. Since the author put more effort into this document, I have found it to be somewhat helpful, if not as a reference, then at least as an overview.
Source: (StackOverflow)
I dislike having magic boxes scattered all over my code...how exactly do these two classes work to allow basically any function to be mapped to a function object even if the function<> has a completely different parameter set to the one im passing to boost::bind
It even works with different calling conventions (i.e. member methods are __thiscall
under VC, but "normal" functions are generally __cdecl
or __stdcall
for those that need to be compatible with C.
Source: (StackOverflow)
If I have a function that needs to work with a shared_ptr, wouldn't it be more efficient to pass it a reference to it (so to avoid copying the shared_ptr object)?
What are the possible bad side effects?
I envision two possible cases:
1) inside the function a copy is made of the argument, like in
ClassA::take_copy_of_sp(boost::shared_ptr<foo> &sp)
{
...
m_sp_member=sp; //This will copy the object, incrementing refcount
...
}
2) inside the function the argument is only used, like in
Class::only_work_with_sp(boost::shared_ptr<foo> &sp) //Again, no copy here
{
...
sp->do_something();
...
}
I can't see in both cases a good reason to pass the boost::shared_ptr by value instead of by reference. Passing by value would only "temporarily" increment the reference count due to the copying, and then decrement it when exiting the function scope.
Am I overlooking something?
Andrea.
EDIT:
Just to clarify, after reading several answers : I perfectly agree on the premature-optimization concerns, and I alwasy try to first-profile-then-work-on-the-hotspots. My question was more from a purely technical code-point-of-view, if you know what I mean.
Source: (StackOverflow)
When I discovered boost::lexical_cast
I thought to myself "why didn't I know about this sooner!" - I hated having to write code like
stringstream ss;
ss << anIntVal;
mystring = ss.str();
Now I write
mystring = boost::lexical_cast<string>(anIntVal);
Yesterday, on stackoverflow, I came across boost split (another gem that will save me writing code).
string stringtobesplit = "AA/BB-CC")
vector<string> tokens;
boost::split(tokens, stringtobesplit, boost::is_any_of("/-"));
// tokens now holds 3 items: AA BB CC
I am going to start looking through boost documentation looking for other functions that I will be able to use regularly, but I feel that it will be very easy to miss things.
What boost functions do you use most / would hate not to have?
Source: (StackOverflow)
Is there a version of 64-bit Boost library for VS2008 ?
Or do I have to compile one myself? if, so, does anyone have experience with it?
Source: (StackOverflow)
When a function should take a shared_ptr
(from boost or C++11 STL), are you passing it
I would prefer the first method because I suspect it to be faster. But is this really worth a though or are there any additional issues?
Could you please give the reasons for your choice or if the case, why you think that it does not matter.
Source: (StackOverflow)
The question is in bold at the bottom, the problem is also summarized by the distillation code fragment towards the end.
I am trying to unify my type system (the type system does to and from from type to string) into a single component(as defined by Lakos). I am using boost::array
, boost::variant
, and boost::mpl
, in order to achieve this. I want to have the parser and generator rules for my types unified in a variant. there is a undefined type, a int4(see below) type and a int8 type. The variant reads as variant<undefined, int4,int8>
.
int4 traits:
struct rbl_int4_parser_rule_definition
{
typedef boost::spirit::qi::rule<std::string::iterator, rbl_int4()> rule_type;
boost::spirit::qi::int_parser<rbl_int4> parser_int32_t;
rule_type rule;
rbl_int4_parser_rule_definition()
{
rule.name("rbl int4 rule");
rule = parser_int32_t;
}
};
template<>
struct rbl_type_parser_rule<rbl_int4>
{
typedef rbl_int4_parser_rule_definition string_parser;
};
the variant above starts out as undefined, and then I initialize the rules. I had a problem, which caused 50 pages of errors, and I have finally managed to track it down, Variant uses operator=
during assignment and a boost::spirit::qi::int_parser<>
cannot be assigned to another (operator=).
To contrast, I don't have a problem with my undefined type:
struct rbl_undefined_parser_rule_definition
{
typedef boost::spirit::qi::rule<std::string::iterator, void()> rule_type;
rule_type rule;
rbl_undefined_parser_rule_definition()
{
rule.name("undefined parse rule");
rule = boost::spirit::qi::eps;
}
};
template<>
struct rbl_type_parser_rule<rbl_undefined>
{
typedef rbl_undefined_parser_rule_definition string_parser;
};
Distillation of the problem:
#include <string>
#include <boost/spirit/include/qi.hpp>
#include <boost/variant.hpp>
#include <boost/cstdint.hpp>
typedef boost::spirit::qi::rule<std::string::iterator,void()> r1;
typedef boost::spirit::qi::rule<std::string::iterator,int()> r2;
typedef boost::variant<r1,r2> v;
int main()
{
/*
problematic
boost::spirit::qi::int_parser<int32_t> t2;
boost::spirit::qi::int_parser<int32_t> t1;
t1 = t2;
*/
//unproblematic
r1 r1_;
r2 r2_;
r1_ = r2_;
v v_;
// THIS is what I need to do.
v_ = r2();
}
There is a semantic gap between concrete parsers and rules. My brain is smoking at the moment so I am not going to think about pramatism, My question is, how do I solve this problem ? I can think of three approaches to solve the problem.
one: Static function members:
struct rbl_int4_parser_rule_definition
{
typedef boost::spirit::qi::rule<std::string::iterator, rbl_int4()> rule_type;
//boost::spirit::qi::int_parser<rbl_int4> parser_int32_t;
rule_type rule;
rbl_int4_parser_rule_definition()
{
static boost::spirit::qi::int_parser<rbl_int4> parser_int32_t;
rule.name("rbl int4 rule");
rule = parser_int32_t;
}
};
I guess approach one prevents thread safe code ? ?
two: The integral parser is wrapped in a shared_ptr. There are two reasons I'm bothering with TMP for the typing system: 1 efficiency, 2 centralizing concerns into components. using pointers defeats the first reason.
three: operator= is defined as a no-op. variant guarantees that the lhs
is default constructed before assignment.
Edit:
I am thinking option 3 makes the most sense (operator= is a no-op). Once the rule container is created it will not change, and I am only assigning to force a type's rule trait into its offset.
Source: (StackOverflow)