I have a simple code with inheritance and shared_ptr
from boost library. With standard c++20, the code compiles fine. The function calls to static_pointer_cast
and dynamic_pointer_cast
compiles without prepended boost::
namespacing -- these function calls work because of ADL (Argument Dependent Lookup).
But with standard c++17, the code will not compile. I would assume that ADL wasn't implemented, or implemented differently, then. But then, if I add using namespace std
, the code compiles fine. My question is: what does std
have to do with boost
library's function call?
Here is the online compiler so you can play around by commenting in and out the using namespace std;
line: https://godbolt.org/z/cz8Md5Ezf
#include <iostream>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
// using namespace std;
class Animal {
public:
Animal()
{}
virtual ~Animal()
{}
void speak()
{
std::cout << "I am an animal\n";
}
};
class Dog : public Animal {
public:
Dog()
{}
void bark()
{
std::cout << "Gheu --> ";
}
void speak()
{
bark();
Animal::speak();
}
};
typedef boost::shared_ptr<Animal> AnimalPtr;
typedef boost::shared_ptr<Dog> DogPtr;
int main()
{
AnimalPtr ap = boost::make_shared<Animal>();
DogPtr dp = boost::make_shared<Dog>();
AnimalPtr ap1 = static_pointer_cast<Animal>(dp);
DogPtr dp1 = dynamic_pointer_cast<Dog>(ap1);
return 0;
}
And if you are thinking, "Well, if using namespace std removes the errors, then those pointer casting calls must be from the std library." I thought so too. But looking through gdb, I saw that no, with using namespace std
, those functions are definitely called from boost library.
use of function template name with no prior declaration in function call with explicit template arguments is a C++20 extension
. This question here seems to be a duplicate of this and this one. – Khersonusing namespace std;
. – Hydroxide