Argument-independent search

Why doesn't the dependency-dependent search take into account Foo::dynamicCastwhether the Foo namespace should be considered, because the Base class is in this namespace?

#include <memory>
using namespace std;

namespace Foo
{

template<typename P, typename T> P*
dynamicCast(T* t)
{
    return dynamic_cast<P*>(t);
}

class Base
{
public:
    virtual ~Base() = default;
};

}

namespace Test
{

class Derived : public Foo::Base
{
};

}

shared_ptr<Foo::Base> b = make_shared<Test::Derived>();
auto d = dynamicCast<Test::Derived>(b.get());
+4
source share
2 answers

, , < >, , ; , , , . , , . , , . , , , , . , . . ?

- ADL , -id. dynamicCast<Test::Derived> not <edit> , dynamicCast , , , .

@T.C. , dynamicCast, ADL.

</edit>

template foo<whatever> . , ++ 20.

+4

ADL. , , .

std::get. get<3>(some_tuple) .

, , args . - , ADL- ( , ADL ).

// tag utilities:
template<class T>struct tag_t{using type=T;constexpr tag_t(){};};
template<class T>constexpr tag_t<T> tag={};

namespace some{
  template<class T, class U>
  T* dynamic(tag_t<T>, U* u){
    return dynamic_cast<T>(u);
  }
  struct bob{};
}

dynamic(tag<int>,new some::bob{}) dynamic ADL.

.

+3

Source: https://habr.com/ru/post/1617489/


All Articles