c++

Working with traits…

URGH!

I’m trying to write something along the lines of:


template<typename _ItrType>
static size_t countThings(_ItrType begin, _ItrType end)
{
  size_t numThings = 0 ;
  for ( _ItrType it = begin ; it != end ; ++it )
  {
    Entity& entity = getEntityFromIterator(it) ;
    numThings += entity.numThings ;
  }
}

// Handle associative iterators.
template<typename _KeyType>
Entity& getEntityFromIterator(std::pair<_KeyType, Entity&>& it)
{
  return it.second ;
}

template<typename _KeyType>
Entity& getEntityFromIterator(std::pair<_KeyType, Entity*>& it)
{
  return *(it.second) ;
}

template<typename _KeyType>
Entity& getEntityFromIterator(std::pair<_KeyType, Entity>& it)
{
  return it.second ;
}

// Handle sequence iterators.
template<typename _ItrType>
Entity& getEntityFromIterator(_ItrType& it)
{
  return *it ;
}

I.e. something that will allow me to write a function that will take an iterator range and worry about whether the iterators are sequence or container for me at compile time.

But it always falls thru to my last case. The error from GCC indicates that I need to specifically look ¬†std::_RB_tree_iterator<std::pair<_KeyType, Entity>> etc, but I’d rather not. I’m wanting to do something to peel back until we get to an Entity, e.g.

template<typename _Outer, typename _Inner>get(_Outer<_Inner>& container) { return *container ; }
template<typename _Key, typename _Value> get(_Key, _Value entity) { return get(entity) ; }
get(Entity& entity) { return entity ; }
get(Entity* entity) { Return *entity ; }

Cannot seem to find a way to get this to work, and I’m starting to think I probably need to do some traits jiggery-pokery. Grr.

Replacing playgate…

Of course, there’s way more important things to do but replacing PlayGate is still on the todo list after my .NET rewrite got scratched due to a failure to understand what “this is a development version so it may require you to install various libraries and stuff” mean’t, and I had to keep the old Win32 C code alive, again.

The thing keeping me from resurrecting my .NET version is that either I’m missing something or .NET localization is a total pain in the backside – even with XAML?

 

Why “auto” is the wrong keyword.

C++0x introduces a new keyword, “auto”, for variable declarations which tells the compiler to infer the variable’s type from the assignment expression.

std::list<std::string> foo = getListOfStrings() ;
// becomes
auto foo = getListOfStrings() ;

While this will save a ton of typing, I think it demonstrates some fundamental issues with the way C++ is being advanced – especially when compared with languages like Python, Erlang and C#.

This handy feature exists in other C-derived languages, in particular C# 4.0 has the same feature but the keyword is different, “var”.

The simple fact is: C++ got it wrong.

Qualm before the sharp.

(Forgot to ‘Publish’ this on the weekend. Re the title: I have no idea…)

I like C#. There. I said it.

It blends the feeling of a scripting language with the power of C/C++. Thanks to Mono, it also has some portability (at least, as far as the scripting bit goes).

In some ways its more like Java than C++. Luckily for me, usually in areas that I actually like about Java (such as interfaces, etc). I also tend to find that things that bothered/bother me about C++ are tackled in C#.

While I’m still a C# greenbelt, I can already see some fairly annoying facets, though.

First, foremost and pettiest:

Technology Review Agrees

Multicore Processors create software headaches.

I guess my recent post was timely :)

Threading Building Blocks intro

I put together a short video walkthru creating a skeleton Visual Studio 2008 project with Intel’s Threading Building Blocks that introduces the basics of using Threading Building Blocks and compares performance of serial and parallel sort.

“DBA” v0.1

http://www.kfs.org/~oliver/dba/
Work-in-progress version of my Database Abstraction project.

C++ Templates, avoiding virtual

I’ve not done a great deal with C++ Templates, and especially with some of the improvements in C++0x, I’m thinking I’d like to change that.

One particular example I’m tinkering with right now is a database abstraction layer I’ve been dragging around with me for years. It has two primary members: DBQuery and DBRow. Up until now they have been littered with #ifdef’s. I want to ditch the ifs and switch to templates.

Mono vs Java

There is a slowly emerging community of .NET developers for Linux in domains that were traditionally strongholds of Java and C++/C developers. Linux people developing with a Microsoft language? The simple “we won’t sue you” promise doesn’t explain it.

And then the penny dropped… Perhaps, Better the Devil You Know?

C++ Closures

The upcoming C++0x standard has already ratified the Lambda Expression concept, which is also being touted as closures. Kinda.

I’m not keen on the syntax, at all. It’s ugly. Even Herb Sutter fumbles with it. The whole thing feels hacky and grumble-grumble-give-them-something that lets you do it.

The problem with a closure is that you want to have your cake and eat it. You want to be able to pass around a pointer to a piece of code to execute, but then you probably want it to be stateful and packaged with a bunch of data.

You can use a struct/class with an operator() functor to implement this, and that’s OK as long as what you are passing the object pointer to knows that’s what you are doing.

But that is not very often the case, especially if you have to go through an API. Infact, function pointers are generally a PITA of C++ programmers.