2

Ok, so I want to know if I would be able to check if a symbol exists inside another file with

#ifdef symbol
    //it exists
#endif

or

#ifndef symbol
    //it doesn't exist
#endif

Reason for that is that I want to check if a method exists in another file(class) and execute additional code accordingly.

EDIT: As far as I've seen, it only checks if a symbol exists in the file(class) where the #ifdef definition is.

EDIT #2: Is there a way to check if a function exists without having to be inside of the class with SFINAE or Templates? If yes, could you provide me some examples? I don't want spoon-feeding, but examples would help.

krentm
  • 132
  • 12
  • 1
    The preprocessor is the wrong tool for something like that. It mainly just deals with simple text substitution and similar - it doesn't actually parse C++. The compiler proper (maybe with something like `if constexpr` in C++17) that actually parses the code, is what you want to look at. – Jesper Juhl Nov 09 '17 at 19:47
  • Well, all I really want is comment(exclude) part of the code if a method exists(which really is text substitution(code with nothing)). If you have any other ideas, could you please let me know? – krentm Nov 09 '17 at 19:50
  • @krentm _"if a method exists"_ Use type traits for checking such. As mentioned you cannot do that with the preprocessor, you'll need to use templates. – user0042 Nov 09 '17 at 19:52
  • @krentm Do you mean to check for the definition of a preprocessor symbol (macro) or existence of a C++ function with a type or namespace? – user0042 Nov 09 '17 at 19:54
  • @user0042 I mean C++ symbol... a method name. – krentm Nov 09 '17 at 19:55
  • 1
    Maybe you are looking for [std::enable_if](http://en.cppreference.com/w/cpp/types/enable_if)? – Jesper Juhl Nov 09 '17 at 19:56
  • @krentm Then refer to my 1st comment. The pre-processor isn't aware of any c or c++ symbols. – user0042 Nov 09 '17 at 19:57
  • Ok, thanks for all the answers. Is there a way to check if a function exists without having to be inside of the class with SFINAE or Templates? – krentm Nov 09 '17 at 19:58
  • 2
    @krentm _"Is there a way to check if a function exists without having to be inside of the class with SFINAE or Templates?"_ Please add that to your question. Won't be possible without templates. The interesting point is, if it's possible to test if a free function exists to enable some code using SFINAE. – user0042 Nov 09 '17 at 20:04
  • @user0042 updated my question. How would I go about using templates from a class, checking if the method(function) exists in another class already? – krentm Nov 09 '17 at 20:09
  • @krentm Seen your update. Does [this](https://stackoverflow.com/questions/22464225/c-detecting-free-function-existence-with-explicit-parameters) help? – user0042 Nov 09 '17 at 20:12
  • @krentm I could write up an answer, that it's definitely not possible using the pre-processor, but you probably should dig down the rabbit hole of templates and SFINAE adapters to find a solution for your actual problem. I'd recommend to change your question completely into that direction (make your use case clear). If you do so, consider the Q&A linked in my previous comment. – user0042 Nov 09 '17 at 20:22
  • @user0042 I'll start learning templates and SFINAE and I'll try to find the solution to my problem. Thanks for all your help and if you write an answer, I'll accept it. – krentm Nov 09 '17 at 20:24
  • @Tsyvarev it's a missclick... sorry – krentm Nov 09 '17 at 20:34

0 Answers0