lundi 28 décembre 2015

Does a map get slower the longer it is

Will a map get slower the longer it is? I'm not talking about iterating through it, but rather operations like .find() .insert() and .at().

For instance if we have map<int, Object> mapA which contains 100'000'000 elements and map<int, Object> mapB which only contains 100 elements.

Will there be any difference performance wise executing mapA.find(x) and mapB.find(x)?

C++ constexpr keyword placement

I have recently started using more C++11 features in my code and I have been wondering if the placement of the constexpr keyword makes difference whether it is before or after the constant's type.

Style 1:

constexpr int FOO = 1;
constexpr auto BAR = "bar";

Style 2:

int constexpr FOO = 1;
auto constexpr BAR = "bar";

Style 2 is the way I prefer to place the const keyword and placing constexpr the same way would bring some consistency to the code. However, is that considered bad practice or is there something else wrong with style 2 because I don't really see anyone writing it like that.

Initialize custom array class without creating copies

I wrote a custom array container, as most C++ libraries including std::array and std::iterator are not available for the target (embedded). The template class is mostly working as intended.
However I did not manage to set up a constructor, which can initialize the whole array without creating copies. I tried several constructors including passing by reference. The one shown will create a copy and has issues if the size of the passed array does not match the one of my class.

Language features up to C++11 are supported in the environment, but no std libraries are available. Therefore the answers to similar questions could not be used.

The sourcecode of the template class:

template<typename T, typename sizetype, sizetype SIZE>
class mArray
{
private:
    T _elements[SIZE]; ///< Array of SIZE elements of Type T
    static_assert( SIZE > 0, "Size must be > 0"); // Discourage empty array
public:
    mArray(){};
    mArray(T initArr[SIZE])
    {
        sizetype index = 0;
        for (auto element : _elements)
        {
            _elements[index] = initArr[index]; //
            ++index;
        }
    }
    T* begin()
    {
        return &_elements[0]; // Pointer to first _element
    }
    T* end()
    {
        return &_elements[SIZE]; // Pointer behind last _elements
    }
    T front()
    {
        return _elements[0]; // Return first element of _elements
    }
    T back()
    {
        return _elements[SIZE-1]; // Return last element of _elements
    }
    T at(sizetype pos)
    {
        static_assert( pos > SIZE, "pos must be < SIZE");
        return _elements[pos];
    }
    T operator[](sizetype pos)
    {
        return _elements[pos];
    }
    T* data()
    {
        return *_elements;
    }
    sizetype size()
    {
        return SIZE;
    }
};

How can I improve the constructor to avoid creating copies?

concept checker doesn't compile on gcc because it 'has no linkage'

I've created a concept checking class based on this question whose purpose is to make sure a given class has a static member function called baseUnitConversionFactor. The class compiles and works fine with msvc2013, but it wont compile on gcc 4.9.2 (using -std=c++14) with the error:

error: ‘{anonymous}::UnitsTest_conceptChecker_Test::TestBody()::validUnit::baseUnitConversionFactor’ is not a valid template argument for type ‘double (*)()’ because ‘static double {anonymous}::UnitsTest_conceptChecker_Test::TestBody()::validUnit::baseUnitConversionFactor()’ has no linkage

static std::true_type test(tester<&U::baseUnitConversionFactor>*);

I don't really know what that means, and am much more familiar with writing templates in visual studios (obviously) much more permisive enviornment. Can anyone help figure out what I need to do to fix this?

Concept Checker Class

template <typename T>
struct has_baseUnitConversionFactor
{
    template<double(*)()> struct tester;

    template<typename U>
    static std::true_type test(tester<&U::baseUnitConversionFactor>*);
    template<typename U>
    static std::false_type test(...);

    static const bool value = decltype(test<T>(0))::value;
};

Test which I think causes the error

TEST_F(UnitsTest, conceptChecker)
{
    struct validUnit
    {
        static inline double baseUnitConversionFactor() { return 0.0; }
        typedef void unit_category;
        typedef void base_unit_type;
    };

    EXPECT_TRUE(has_baseUnitConversionFactor<validUnit>::value);
}

How can be used returned temporary link in best way?

  • for example I have

    class A {
         AHelper&& getAHelper(){
             return AHelper();
        }
    }
    
    int otherfunc(){
        A a;
        xfunc( a.getAHelper() );
    }
    
    

    May I use it only if I want to pass it to another function? What are benefits?

How to specify function signature for function that returns result of async?

I have a function that does this:

??? createThread(int x) {
   return async(std::launch::async, 
      [x] () { // do stuff with x });
}

I am using VS 2012 (partial C++11). What should the return type be to make this compile?

getting the type of operator() from a bind versus a lambda function

Please take a look at the following code below. In the case of a capture lambda expression it will work and compile just fine. Although when using a bind callable expression I will get an overload expression expression error

main.cpp:13:60: error: decltype cannot resolve address of overloaded function 
 using FuncType = decltype(&std::decay<T>::type::operator()); 

How can I code this such that I am getting the type of the operator() method for the bind function? The bind type is std::_Bind<void (*(std::_Placeholder<2>, std::_Placeholder<1>))(int, int)> which I also don't fully understand; I know void (*) (int, int) as a type as a pointer to function taking in int, int and returning void. I guess I don't understand the syntax void *(x,y) (int,int); basically the x,y part. I was assuming that after all the template stuff there is a method void operator()(int, int) that will get resolved that bind_f(x,y) will call and I was trying to capture that type.

#include <iostream>
#include <functional>
#include <type_traits>

void tester(int x, int y) {
   std::cout << " x = " << x << " y = " << y << std::endl;
}

template <typename T>
class TypeChecker;

template <typename T>
using FuncType = decltype(&std::decay<T>::type::operator());

int main()  {
   using namespace std::placeholders;
   auto bind_f = std::bind(tester, _2, _1);
   bind_f(1,2);                            

   int y = 5;                              

   auto lambda = [y]() {                   
      std::cout << " y = " << y << std::endl;
   };                                        

   typedef FuncType<decltype(lambda)> x1;            
   typedef FuncType<decltype(bind_f)> x2;            

   //TypeChecker<decltype(bind_f)> t2;       

}