Boost logo

Boost Users :

From: Andrea Bocci (andrea.bocci_at_[hidden])
Date: 2022-05-02 06:40:26


Hi Warren,

On Mon, 2 May 2022, 03:29 Warren Weckesser via Boost-users, <
boost-users_at_[hidden]> wrote:

>
> In a program like this:
>
> ```
> #include <boost/math/distributions/beta.hpp>
>
> using boost::math::beta_distribution;
>
> int main()
> {
> float a = 1.5f;
> float b = 3.0f;
> beta_distribution<float> dist(a, b);
>
> auto m = mean(dist);
> }
> ```
>
> how does the name 'mean' end up in scope in 'main()'?
>

this is indeed a C++ question: what you are observing is called "argument
dependent lookup".

Since `dist` is of a type defined in the `boost::math::beta_distribution`,
the compiler will look up `mean` in the same namespace, and finds a
definition there.

If you try using `mean` with a different argument the full namespace should
be needed:
```
std::vector<double> v{1,2,3,4,5};
double mu = boost::math::statistics::mean(v);
```

For more information you can check
https://en.cppreference.com/w/cpp/language/adl .

Hope this helps,
.Andrea

>



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net