109 Posted Topics
Studying modern c++ design, looks like Typelist is a very important tool for generic programming. Could I find something like Typelist from boost? I found something like boost::mpl but I didn't see anything like Nulltype defined in loki Could boost::mpl replace the Typelist of loki? Thanks | |
I don't my deduction of NoDuplicates are correct or not please correct my error if I did [code] template<typename TList> struct NoDuplicates; template<> struct NoDuplicates<NullType> { typedef NullType Result; }; template<typename Head, typename Tail> struct NoDuplicates<Typelist<Head, Tail> > { //private : typedef typename NoDuplicates<Tail>::Result L1; typedef typename Erase<L1, Head>::Result L2; … | |
How could we measured the relationship between types and binary files generated by compilers? [code] template<typename T> T Max(T const A, T const B) { return A > B ? A : B; } void testMax() { int a = 5, b = 10; double c = 44.4, d = … | |
compiler : visual c++ 2010 os : win7 64bits [CODE] std::string temp = "temp"; boost::tuple<std::string> A( std::move(temp) ); //this is copy std::tuple<std::string> A( std::move(temp) ); //better, woundn't compile [/CODE] this is inconvenient when I want to do something like this [code] std::tuple<std::string> kkk() { std::string temp = "temp"; return std::make_tuple( … | |
Re: [QUOTE=;][/QUOTE] forget this codes, just pretend it never exist | |
Re: [QUOTE=;][/QUOTE] key word : std::greater if std::greater can't meet your need, you could try key words : functor, lambda expression, std::function, std::bind | |
Re: If you don't mind to co-work with old style C++ you could consider about xerces c++ I heard that boost also have a tool which could help you parse XML maybe you could take a look on boost and xerces c++ | |
Boost provide us [code]is_base_of[/code] and [code]is_polymorphic[/code] Yet the dynamic type need to be deduce on running time What kind of situations would you use [code]is_base_of[/code] and [code]is_polymorphic[/code] to replace dynamic_cast? Thank you very much | |
[code] template<typename U, typename T, typename binary_functor> struct template_param_swap { binary_functor<U, T> func1; #1 binary_functor<T, U> fun2; #2 }; [/code] CH3 of modern c++ design may solve this, but I am not a template wizard and don't have the ability to make it yet. template template parameter could make #1 … | |
For what I know, typeid(type) would return a type_info object, typeid(type).name() would print the name of the type, the names are depend on compilers. I would like to build a table by std::map<typeid(type).name(), std::function>. My questions : (1) : For what I know, the names of typeid(type).name() are not always … | |
Re: new is designed for general purpose, it is good for big object(like 1024 bytes or more) the benefits of new over malloc are type safe and could be overwrite but new may squander a lot of spaces when you deal with small objects if you need to allocate small objects … | |
Since the data structure are something like [code] struct morph { size_t num; std::string name; //and so on } [/code] I have to find the different of the two sets with functor or some 3rd party where could I find something like this? Below is the implementation alter by me(refer … | |
Re: I don't know what are you trying to do But a dynamic struct within a struct is pretty easy [code] struct student_info { std::string name; size_t id; .......and so on }; class student_handle { public : //define the function you want private : std::vector<student_info> data_;//dynamic array of struct student_info }; … | |
Re: better prefer pass by reference rather than pass by value there are several easier ways to do this [code] std::copy(user.rbegin(), user.rend(), std::back_insert(rev) ); [/code] or [code] std::reverse_copy(forward_.begin(), forward_.end(), std::back_inserter(backward_) ); [/code] or [code] std::string forward = "abcdefg" std::string backward(forward.rbegin(), forward.rend() ); [/code] I would prefer the last way, it is … | |
Is it really hard to find someone who know how to make a good use of stl and combined the power of GP and OOP?Like the way modern c++ design did? There are so many people say these kind of techniques are unrealistic because there are only a few of … | |
Re: your code doesn't looks like the c++ I familiar with where is your "resize" come from? and what is ref? [code] Array.Resize(ref myArray, length); [/code] besides, [code] int[] myArray = new int[4]; [/code] is not the way to declare one dimension array you should use [code] int *myArray = new … | |
I am a newbie who just graduated. I found my first job and begin to review some legacy codes And I find out there are a lot of macros which could be replaced by function (somebody especially c coders may think macro is a better choice) Not even that, I … | |
[code] void test(std:shared_ptr<some_type> A) {.....} [/code] would make a copy of a shared_ptr what if we pass a shared_ptr into a function like [code] void test(std:shared_ptr<some_type> const &A) {.....} [/code] Is this safe? Thanks a lot | |
[code] void test_rr(std::string const &lvalue) { std::cout<<"this is lvalue"<<std::endl; } void test_rr(std::string &&lvalue) { std::cout<<"this is rvalue"<<std::endl; } int main() { test_rr("this is"); //will output "this is lvalue" std::cout<<"system pause"<<std::endl; std::cin.get(); return 0; } [/code] how could I treat "this is" as rvalue reference? The easiest way I could think … | |
I have to learn how to deal with socket programming because of my job(I don't have any experience about it) My company ask me to learn ACE because I have to maintain our ex and new products After I google the information from the internet, most of the users say … | |
Re: [QUOTE=;][/QUOTE] you don't need to define TRUE and FALSE c++ already prepare for the built in true and false | |
compiler : gcc4.6.2(minGW), vc2010 os : win7 64bits [CODE] int A[10]; srand(0); std::generate(A, A + sizeof_array(A), [](){ return rand() % 100; } ); std::vector< std::unique_ptr<int> > v; std::for_each(A, A + sizeof_array(A), [&](int const Cur) { v.push_back(std::unique_ptr<int>( new int(Cur) )); } ); std::sort( v.begin(),v.end() ); //result {1,4,8} for(auto it = v.begin(); … | |
I do some experiences about boost::bind And I find out that boost::bind would incur heavy copy if you don't pass by referene [code] #include<iostream> #include<boost/bind.hpp> struct test_for_bind_00 { test_for_bind_00() {} test_for_bind_00(test_for_bind_00 const &Obj) { std::cout<<"this is copy constructor :: tfb00"<<std::endl; } }; struct test_for_bind { void tamaya() { std::cout<<"tamaya"<<std::endl; } … | |
Re: you could wrap it in one while loop you may use if...else and break to solve this question a simple example [code] while(1) { if(num < xx) ..... ...... else if(num == yy) break; } [/code] | |
compiler : visual c++ 2010 os : windows 7 64bits learning template and encounter some problem [code] template<typename T> void easyCopyImpl(T const &A, typename boost::enable_if_c< boost::is_array<T>::value>::type *temp = 0) { std::cout<<"this is array"<<std::endl; std::cout<<typeid(T).name()<<std::endl; std::copy(A, A + sizeof_array(A), typename std::ostream_iterator<T>(std::cout, "") ); } template<typename T> void easyCopyImpl(T const &A, typename … | |
Re: [CODE]char ch; cin>>ch; if(ch=='SU') { run this code..etc }[/CODE] first : char only hold one character second : '' only work on single character but "" for string(multiple characters) third : the built in type [code]char[/code] do not support "==" | |
Re: [quote]you may want to write your code in such a way that this issue is addressed. [/quote] I would rather use type_traits to avoid the problem of code bloat. type_traits is ease to use, although the syntax is a little bit verbose | |
Below is part of the source code of an allocator from the sgistl(2.91.57) [CODE] void ( *set_malloc_handler( void(*f)() ) ) () { } [/CODE] The purpose of this function is mimic the behavior of set_new_handler. But i don't know what is the meaning of this function. return type = void … | |
At first, I wrote something like this [code] template<typename T, size_t size> inline size_t const sizeof_array(T const [size]){return size;} [/code] but if failed, then I find a solution form the other place, like this. [code] template<typename T, size_t size> inline size_t const sizeof_array(T const (&)[size]){return size;} [/code] what is the … | |
Currently I am studying multi-thread, and I feel pretty confusion about the concept of blocking, non-blocking, synchronous and asynchronous. Exactly, there are so many explanations, and each of them have something differents, someone says synchronous could be non-blocking too, someone says synchronous must be blocking.Someone says asynchronous must be non-blocking, … | |
What is the meaning of using dynamic_cast without any error handling? Like [code] struct base { virtual void testing() { std::cout<<"base"<<std::endl; } }; struct derived : public base { virtual void testing() { std::cout<<"derived"<<std::endl; } }; int main() { base baseInstance; derived derivedInstance; base *bp = &derivedInstance; base &br = … | |
[code] void testRR(std::string &A); [/code] [code] void testRR(std::string &&A); [/code] Which one is better? && is more flexible than &, should I choose && rather than &? Thanks | |
[CODE] boost::shared_ptr<resource> resource_ptr; boost::mutex resource_mutex; void test() { if(!resource_ptr) //#1 { boost::lock_guard<boost::mutex> lk(resource_mutex); if(!resource_ptr) //#2 { resource_ptr.reset(new resource); //#3 } } resource_ptr->do_something(); } int main() { boost::thread t1(test); boost::thread t2(test); t1.join(); t2.join(): return 0; } [/CODE] According to the text book, this kind of codes may cause race condition, but … | |
I am learning concurrency programming, I try my best yet can't solve the problem [CODE] boost::mutex mut; std::vector<int> int_vector; boost::condition_variable int_cond; void data_preparation_thread() { while(int_vector.size() != 10) { std::cout<<"data preparation thread :: vector size = "<<int_vector.size()<<std::endl; boost::lock_guard<boost::mutex> lk(mut); int_vector.push_back(4); int_cond.notify_one(); } } void data_processing_thread() { while(1) { std::cout<<"front :: data … | |
I found some implementation by macro with the help of macro I may write something like [code] #define DEFINE_LIST(type) \ typedef struct list_node_##type { \ struct list_node_##type *next; \ type data; \ } int main() { DEFINE_LIST(int); // defines struct list_node_int DEFINE_LIST(double); // defines struct list_node_double IMPLEMENT_LIST_INSERT(int); // defines list_int_insert … | |
something like [code] class base { public : virtual void draw() const = 0; virtual void area() const = 0; }; class rectangle : public base { public : virtual void draw() const {std::cout<<"rectangel\n";} virtual void area() const {std::cout<<"rect area\n";} }; class triangle : public base { public : void … | |
As far as I know, if we don't ask template to generate the code for us It would not do it because template is "lazy". The codes generated by template are same as handcrafted codes, so what makes template being blamed by code bloat if you know how to handle … | |
I have some problems about promotion traits [code] template<typename T1, typename T2> class Promotion; template<bool C, typename Ta, typename Tb> class IfThenElse; template<typename Ta, typename Tb> class IfThenElse<true, Ta, Tb> { public : typedef Ta ResultT; }; template<typename Ta, typename Tb> class IfThenElse<false, Ta, Tb> { public : typedef Tb … | |
compiler : g++ of gcc4.5.2(minGW) os : windows xp sp3(32bits) [code] template<typename T, template<typename> class CONT = AccumulationTraits > class Accum { public : static typename CONT<T>::AccT accum(T const *beg, T const *end) { typename CONT<T>::AccT total = CONT<T>::zero(); while(beg != end) { total += *beg; ++beg; } return total; … | |
compiler vc2010 and gcc4.5.2(minGW) os : windows xp sp3 I am trying to implement a generic find_if function as std::find_if Below is my code [code] //code for estimating expanding time template<typename unaryOP> double timeCount(unaryOP op, size_t const LOOP = 1, char const* NAME = "function") { std::clock_t begin, end; begin … | |
Sorry I don't know how to express it in English, so I use some codes to show what I am trying to say [code] template<typename strategyOne, typename strategyTwo, typename strategyThree> void someCode(strategyOne stOne, strategyTwo stTwo, strategyThree stThree) { //some codes stOne(some args); //some codes stTwo(some args); //some codes stThree(some args); … | |
my compilers::tdm gcc4.5.2 my os::windows xp sp3(32bits) boost 1.45 Sorry, I have another problem again [code] template<typename accStrategy = float> int const adaptSR(int const MAX_AL, float const CONFIDENT_LV, int const OFFSET, accStrategy PLAN) { return 1; } template<typename accStrategy = float> int const adaptSR(int const MAX_AL, float const CONFIDENT_LV, int … | |
As I know, perfect forwarding is design for reducing the need of many overloaded functions when you pass the reference to the function and pass those reference to another functions Besides, to fullfill the objective of perfect forwarding && should be able to swallow the lvalue and rvalue That means … | |
[code] std::tr1::array<double, 10> alpha; std::iota(alpha.begin(), alpha.end(), 0); using namespace boost::accumulators; size_t const SIZE = 1000000; accumulator_set<double, stats<tag::mean, tag::lazy_variance > > acc; std::for_each(alpha.begin(), alpha.end(), [&](double const VALUE){ acc(VALUE);}); [/code] How could I clear the contents of the acc if I want to evaluate a new mean and variance? Thank you very … | |
[code] template<typename anyFunctor = printTxt> //printTxt is somekind of functor void testFunctor() { anyFunctor f; } void testFunctor2() { testFunctor(); } [/code] 1 : if [b]type anyFunctor[/b] need 3 arguments to initialize it, what should I do? Do I have another way to make this become more adaptable except of … | |
my os : windows xp sp3 compilers : gcc 4.5(minGW) IDE : code blocks 10.05 As the title, when I didn't enable the command [b]-std=c++0x[/b] CImg can work with gcc4.5 and code block(after Build Options > Linker Settings > Link libraries > Add and enter gdi32) Or use this kind … | |
compiler gcc4.5 minGW OS : windows xp sp3 I try something like this [code] template<typename T, typename U> class fastRowSum { public : fastRowSum(); typedef void result_type; //what is this line for? template<typename inputItr, typename outputItr> void operator() (T const VALUE, inputItr ref, outputItr out) { error = VALUE > … | |
[code] std::copy (std::istream_iterator<string>(cin), // beginning of source std::istream_iterator<string>(), // end of source std::ostream_iterator<string>(cout,"\n")); // destination [/code] how could I quit from it? besides, if I want to copy partial data from std::ifstream what should I do with the copy algorithm? following code would copy all of the data into the … | |
I am building a simple tree by pointer this tree would only have one child and one sibling if you call addChild more than once on the same node, it would add a sibling if you call addchild more than two times(assume it is three times) on the same node … | |
Below is my code [code] template<typename iterator> void selSort(iterator begin, iterator end) { iterator minIt = begin; iterator it2 = begin + 1; for(iterator it = begin; it != end; ++it) { minIt = it; for(; it2 != end; ++it2) { if(*it2 < *minIt) *minIt = *it2; //line X } … |
The End.