1

My question is of a purely organisational nature and hence I realize potential answers may be subjective in nature. After years of working with C#, I've finally returned to C++ and am struggling with getting used to how to properly organise the files.

Basically, I want to know where the best places are to define predicates and function objects. Currently, I'm using a simple function object for an STL algorithm in a single class. This function object will not be useful to any other class, so where do I put it? Do I place it in the same namespace and in the header file or in the .cpp file? Do I place it in an anonymous namespace or do I put it in its own header/cpp file?

Thank you in advance for any and all answers,

Kevin

Renegade Vile
  • 157
  • 10

4 Answers4

1

It really depends what the predicate is, if it might have any use to the end user I would put it's declaration in the header, ideally as a free function taking by const ref, and the implementation in the source file.

If it's a part of a program (rather than library) then I would just do the same too for simplicities sake.

If it is strictly for internal use and you do NOT want the end user using it then you can place it in an anonymous namespace in the source file.

111111
  • 15,686
  • 6
  • 47
  • 62
  • I see, alright, these guidelines are easy to remember and follow, thank you for the answer. In this case, it's likely most appropriate to place it in an anonymous namespace in the source file. I've been using C# for so long that I've simply forgotten how to properly organize C++ code (the absence of header files and all that). – Renegade Vile Mar 07 '13 at 12:02
1

I assume you can't use C++11 otherwise I would suggest to use lambdas wherever possible ;) however since I don't have good arguments here is just what I usually do:

If I only use the predicate in a single function I declare it in the function, If I use it in only one code file, I put it in an anonymouse namespace. I like to keep the .h files as clean as possible to reduce the amount of rebuilds and file dependencies.

I beleive using anonymouse namespaces or putting them into a function helps avoiding name collisions and results cleaner code.

chris g.
  • 162
  • 1
  • 4
  • That's why I was leaning towards anonymous namespaces in the source file before putting up this post. Thank you for the help! – Renegade Vile Mar 07 '13 at 12:07
1

Depends on the predicate's use. Since you say it isn't useful anywhere else, it's best to declare it in your cpp file so as to not clutter the namespace when people include your header file.

If it isn't even useful at file scope, you could consider using lambdas if it's not too complicated. In case you don't have access to C++11's lambda's (or Boost.Lambda) you can declare local functors even at function scope. This is pretty handy! However, remember that you cannot have templated functors at file scope level which is often what you would want though (an example hereof would be the use of 'templated iterators').

dgrine
  • 702
  • 6
  • 15
  • Alright, .cpp it is. And I know how to work with lambdas (C# has them, after all) but I had no idea it had already been incorporated into C++! I'll have to definitely look into this, because most applications I create only need simple, non templated functors, so lambdas are ideal. Thank you for the help. – Renegade Vile Mar 07 '13 at 12:04
1

If it's meant to be used just by one class, and you don't expect it to be useful to anyone else, that sounds like exactly the time for a Nested Class:

class SomeClass {
public:
    //...

    struct MyPredicate {
        bool operator() (const SomeData&) const;
    };

};

The actual name of the class above is SomeClass::MyPredicate, so if the operator() is not defined at its declaration, you'll need to define SomeClass::MyPredicate::operator() somewhere. But inside any member of SomeClass you can just type MyPredicate.

You could also make the predicate a private member of its containing class, in which case no outside code is even allowed to type the name of that class.

aschepler
  • 70,891
  • 9
  • 107
  • 161