0

I'm using std::any, which works well in cpp file, but error errors when used in header file. The C++ document doesn't state any could be only used in cpp files.

Example code test.h

#include <iostream>
#include <map>
#include <any>

using namespace std;

typedef map<string, any> any_map;    // error: use of undeclared identifier 'any'

Environment : Clang12, C++17

Thanks a lot.

frogcdcn
  • 391
  • 1
  • 4
  • 14
  • https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice?rq=1 – Passerby Nov 06 '21 at 02:05
  • If you prefix all types with `std::` namespace like `typedef std::map any_map;` does it solve the issue? – Arty Nov 06 '21 at 02:07
  • 3
    There is no difference between cpp and header files as far as the compiler is concerned, so your issue is caused by something else you haven't shown here. I'm guessing you didn't actually compile as c++17. – interjay Nov 06 '21 at 02:11
  • 2
    compile using `std=c++17` flag. – Abhishek Dutt Nov 06 '21 at 02:20
  • thanks all. With ```std::any```, it works in my test code. CMake prompts it's using C++17. – frogcdcn Nov 06 '21 at 03:07

0 Answers0