Using std::pow inside stan::math namespace

using declarations (namespace or individual names) inside header files that change what namespace a function is pulled from are generally only used inside functions, for the simple reason that if it were instead done at file scope (and therefore at translation unit scope), any header files included after the declaration (and therefore hierarchically higher ) have the meaning of their unprefixed symbols possibly changed, without any corresponding change in the source code itself. In other words, it changes the meaning of code based on the semi-random order in which you write your includes (see example below). You may think this is easy to avoid, but I assure you it is not. My opinion: put the using declarations as close to where they are used as possible: inside function bodies. Also, the other functions rok mentioned should be candidates in the future for being rewritten to not do this.

example:
—some_deep_file.h—

using foo::a;

—top_level_file_1.h—

include "some_high_level_file_that_eventually_inludes_some_deep_file.h"
include "some_other_high_level_file.h" // meaning of "a()" is modified

—top_level_file_2.h—

include "some_other_high_level_file.h" //meaning of "a()" is normal
include "some_high_level_file_that_eventually_inludes_some_deep_file.h"

BTW, this is the kind of thing that C++ modules is meant to address.

4 Likes