-1

I am a C# programmer who is learning OpenCV in C++ with Visual Studio 2017. In C# we can invoke CTRL+. to automatically activate using directive as long as the corresponding assembly are already referenced in the project.

In a book I am reading, the author always shows the code snippet without saying what headers I have to include. For example, he wrote

void salt(Mat& image, int n)
{
    std::default_random_engine generator;
    // others are removed for the sake of brevity.
}

When I found the prefix std, I always thought that the function must be in iostream. After searching, std::default_random_engine is apparently in random header file.

Question

How do we know in which header does a function exist (with VS 2017)? How can I know that namespace std also includes random in addition to iostream.

Miki
  • 40,887
  • 13
  • 123
  • 202
Second Person Shooter
  • 14,188
  • 21
  • 90
  • 165
  • 1
    Note that `` is a header and there is no identifier call `random` in the `std` namespace. – François Andrieux Jan 08 '19 at 19:54
  • In VS you could explore the function 'Find in Files' – engf-010 Jan 08 '19 at 20:04
  • @engf-010: But the type has not been recognized yet until we include `random`. – Second Person Shooter Jan 08 '19 at 20:05
  • 1
    "How do we know in which header does a function exist" - We look it up in documentation. [cppreference.com](https://cppreference.com/) is one excellent place, another is a [good book or two](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). There's also [the standard](http://eel.is/c++draft/) itself. – Jesper Juhl Jan 08 '19 at 20:08
  • @FrançoisAndrieux: I am really confused with the taxonomy of C++ libraries when talking about the namespace `std`. Among others, are both `cout` (from `iostream` header) and `default_random_engine` (from `random` header) in the same namespace `std`? – Second Person Shooter Jan 08 '19 at 20:10
  • 1
    @GodMustBeCrazy Yes. Most content in the standard library is in namespace `std`, meaning you either have to qualify them with the `std` namespace (like `std::cout`) or pull the entire namespace (with `using namespace std`, not recommended). You can also pull individual elements (with `using std::cout;` for example). This avoids any conflict between standard identifiers and your identifiers (you could write your own `cout` thing outside of `std`). Header files and namespace are orthogonal. Namespace has no impact on where and how headers are found during compilation. – François Andrieux Jan 08 '19 at 20:13
  • @FrançoisAndrieux: I am skimming the `random` header file and I did not find the enclosing `namespace std{...}`. Where does the enclosing `namespace std{}` exist? – Second Person Shooter Jan 08 '19 at 20:17
  • 1
    @GodMustBeCrazy Who knows. Anything inside a standard header is an implementation detail and can change from compiler to compiler, from version to version and even build configuration to build configuration. And that's *if* there is an actual file representing it (the implementation may behave as-if there was such a file, but actually rely on an optimized internal representation instead). It might be that the `namespace std` statement is hidden inside a macro or is otherwise obfuscated. On my platform, it happens that it's hidden in a `_STD_BEGIN` macro. – François Andrieux Jan 08 '19 at 20:19
  • 1
    @GodMustBeCrazy Note that standard headers are *not* intended to be read. They have restrictions on the identifiers they use, they can make use of unique compiler intrinsics and almost any concern has a higher priority than readability. They can be quite hard to parse. Edit : It's quite the opposite of what you should try to do when you write headers, which should act as a form of documentation and be as easy to understand as is practical. – François Andrieux Jan 08 '19 at 20:21
  • @FrançoisAndrieux: Thank you for elaborating. C++ is interesting and confusing at the same time. :-) – Second Person Shooter Jan 08 '19 at 20:22
  • 1
    @GodMustBeCrazy I agree completely with that sentiment. – François Andrieux Jan 08 '19 at 20:22
  • 1
    @GodMustBeCrazy I've just noticed that I answered a question [here](https://stackoverflow.com/a/42233717/7359094) explaining namespaces. – François Andrieux Jan 10 '19 at 18:18

1 Answers1

2

I will usually search for the class on cppreference, which shows which headers must be included.

Alternatively, you can use a static analysis tool like include-what-you-use.

0x5453
  • 12,753
  • 1
  • 32
  • 61
  • Sorry: Why is there no `iostream` and `random` in `std` [here](https://en.cppreference.com/mwiki/index.php?title=Special%3ASearch&search=std)? – Second Person Shooter Jan 08 '19 at 20:02
  • I am really confused with the taxonomy of C++ libraries. – Second Person Shooter Jan 08 '19 at 20:03
  • @GodMustBeCrazy I never understood how to properly use their search, just google it and the first two hits will be cppreference and a second reference page – 463035818_is_not_an_ai Jan 08 '19 at 20:04
  • @GodMustBeCrazy Just search for "default_random_engine" on that site and you'll find what you need. (But don't use `default_random_engine` though, you don't know what you'll get - better to use a *specific* engine explicitly). – Jesper Juhl Jan 08 '19 at 20:14
  • @GodMustBeCrazy That search term should return a ton of stuff, looks like the results are truncated. And you wouldn't find headers in that search since headers aren't related to `std`. Headers and namespaces are unrelated concepts. Headers contain code that may define or refer to namespaces, but that is the full extent of the relationship. – François Andrieux Jan 08 '19 at 20:15
  • 2
    @GodMustBeCrazy [Here](https://en.cppreference.com/w/cpp/header) is a list of standard headers on cppreference.com – François Andrieux Jan 08 '19 at 20:16
  • 1
    @GodMustBeCrazy for a list of every name in std::, http://en.cppreference.com/w/cpp/symbol_index – Cubbi Jan 08 '19 at 20:52