how can i test if a function is defined?

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

46 Replies - 2325 Views - Last Post: 11 May 2018 - 07:38 AM Rate Topic: ***-- 2 Votes

#1 Cambalinho   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 762
  • Joined: 04-March 12

how can i test if a function is defined?

Posted 29 April 2018 - 08:12 AM

if i call 1 function that have a prototype, but not defined, the compiler will give me 2 errors.
using Pre-processor directives, can i test if a function is defined or not?
(is for i test what function i can call)
i tried these code without success :(
#ifdef clsPointer->MouseClick
            clsPointer->MouseClick();//the function is called if is defined
        #else
            MouseClick();//these function is called, if the other isn't defined
        #endif // clsPointer

because the clsPointer->MouseClick isn't defined, for the pre-processor.
these is for avoid 1 compiler error.
can anyone advice me?

Is This A Good Question/Topic? 0
  • +

Replies To: how can i test if a function is defined?

#2 Salem_c   User is online

  • void main'ers are DOOMED
  • member icon

Reputation: 2258
  • View blog
  • Posts: 4,347
  • Joined: 30-May 10

Re: how can i test if a function is defined?

Posted 29 April 2018 - 08:17 AM

Also here -> https://cboard.cprog...on-defined.html
Was This Post Helpful? 2
  • +
  • -

#3 Cambalinho   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 762
  • Joined: 04-March 12

Re: how can i test if a function is defined?

Posted 29 April 2018 - 08:24 AM

that's true that i'm on several forums. some answer and others don't.
i just need some help.
it's bad registed on several forums?
Was This Post Helpful? 0
  • +
  • -

#4 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2681
  • View blog
  • Posts: 4,281
  • Joined: 21-June 11

Re: how can i test if a function is defined?

Posted 29 April 2018 - 08:36 AM

It's perfectly fine to be registered in as many forums as you want, but it's a bit annoying if you ask the same question in several places at the same time. It just leads to people repeating themselves and wasting time (especially if you already got answers or requests for clarification on the other forum and don't mention those here (or vice versa)).

Anyway, no, there's no general way to check whether a function is defined or not. But why do you need to check that? Do you only define the function on some platforms or under certain conditions? In that case you can just check those conditions again.

In general you should never declare a function and then not define it (unless it's defined elsewhere of course). If you only define it under some circumstances, you should also only declare under those circumstances. You can then also provide a preprocessor constant to check whether the function exists rather than repeating the conditions all over the place.

However I feel like you probably don't need this at all and are approaching your problem the wrong way. So maybe you can tell us more about what you're trying to do.
Was This Post Helpful? 3
  • +
  • -

#5 Cambalinho   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 762
  • Joined: 04-March 12

Re: how can i test if a function is defined?

Posted 29 April 2018 - 09:01 AM

i'm creating a macro with all virtual functions, for not repeat them on derived class(where they are virtual).
(for not repeat some code, and define them outside de class. in these case, these class's can't have instances)
if i prototype a function i must define it or the compiler give me 2 errors.
i can create 10 different derived class's, with different macros... but i need with 1 universal macro(these macro have only the functions prototypes)

i'm trying these way:
std::function<void()> func{InstanceName.MouseClick};
    if(func)
        std::cout<< "defined";
    else
        std::cout<< "not defined";

'InstanceName' it's a class name... inside the 'func', i can't call the function.
so how can i fix the error?
error: "no matching function for call to 'std::function<void()>::function(<brace-enclosed initializer list>)'"
Was This Post Helpful? 0
  • +
  • -

#6 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2681
  • View blog
  • Posts: 4,281
  • Joined: 21-June 11

Re: how can i test if a function is defined?

Posted 29 April 2018 - 09:09 AM

Yeah, none of that is going to work. You'll need a definition for every function to declare. If you don't provide a definition, you must not declare it. There's no way around that.

You can certainly not check at run time whether a function was defined. If there are undefined functions, linking will fail, so there will be no executable, so there will be no run time.
Was This Post Helpful? 0
  • +
  • -

#7 Cambalinho   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 762
  • Joined: 04-March 12

Re: how can i test if a function is defined?

Posted 29 April 2018 - 09:18 AM

i'm sorry, but why i can't add the function on lambda?
it's a function
Was This Post Helpful? 0
  • +
  • -

#8 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2681
  • View blog
  • Posts: 4,281
  • Joined: 21-June 11

Re: how can i test if a function is defined?

Posted 29 April 2018 - 09:44 AM

You can't create std::functions from member function pointers - only normal function pointers. But that doesn't really matter. If you could do that (or if you were using a free function instead of a member function), you'd just be back where you started anyway: You'd get a linker error that MouseClick does not have a definition.
Was This Post Helpful? 0
  • +
  • -

#9 Cambalinho   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 762
  • Joined: 04-March 12

Re: how can i test if a function is defined?

Posted 06 May 2018 - 06:23 AM

finally i have something:
template <class Type>
class TypeHasToString
{
    // This type won't compile if the second template parameter isn't of type T,
    // so I can put a function pointer type in the first parameter and the function
    // itself in the second thus checking that the function has a specific signature.
    template <typename T, T> struct TypeCheck;
    typedef char Yes;
    typedef long No;

    // A helper struct to hold the declaration of the function pointer.
    // Change it if the function signature changes.
    template <typename T> struct ToString
    {
        typedef void (T::*fptr)();
    };
 
    //yes these line is limited... it must be updated after fix the error
    template <typename T> static Yes HasToString(TypeCheck< typename ToString<T>::fptr, &T::MouseClick >*); //function name... 
    template <typename T> static No  HasToString(...);

public:
    static bool const value = (sizeof(HasToString<Type>(0)) == sizeof(Yes));
};

before i ask for update these template, i need more help for fix it :(
1 - if the function have a prototype and defined or have a prototype and not defined or just defined inside de class, the result will '1'.... until here seems ok;
2 - if i use a function prototype, without defined, i will get '1' too... and these is the problem.
so how can i fix these template for test, only, defined function?
Was This Post Helpful? 0
  • +
  • -

#10 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2681
  • View blog
  • Posts: 4,281
  • Joined: 21-June 11

Re: how can i test if a function is defined?

Posted 06 May 2018 - 06:28 AM

You can't. If a function is declared, then, as far as the compiler is concerned, the function will be defined at some point. Only the linker will know which functions are and aren't defined - there is no way for the compiler to access that information. And even if there were, there are no facilities in C++ to query that information.

So as I said before: You can't do what you're trying to do. It's not possible. If you declare functions that you do not define, you're doing things wrongly and you will run into problems. You can't avoid that.
Was This Post Helpful? 2
  • +
  • -

#11 Cambalinho   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 762
  • Joined: 04-March 12

Re: how can i test if a function is defined?

Posted 06 May 2018 - 07:03 AM

i understand what you mean. but what you can advice me more?
(maybe i can find more options)
Was This Post Helpful? 0
  • +
  • -

#12 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6565
  • View blog
  • Posts: 22,528
  • Joined: 05-May 12

Re: how can i test if a function is defined?

Posted 06 May 2018 - 07:06 AM

Why don't you start off with telling us what feature you are trying to provide for users of your library instead of going into the weeds with needing to detect if a function is declared, but not defined?
Was This Post Helpful? 3
  • +
  • -

#13 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2681
  • View blog
  • Posts: 4,281
  • Joined: 21-June 11

Re: how can i test if a function is defined?

Posted 06 May 2018 - 07:09 AM

The only advice I can give you is not to generate declarations for functions that you do not define. You'll have to rethink how you're using macros to generate those classes. I can't really offer more than that without knowing what your classes look like or why you feel you need macros to generate them in the first place.
Was This Post Helpful? 1
  • +
  • -

#14 Cambalinho   User is offline

  • D.I.C Addict

Reputation: 0
  • View blog
  • Posts: 762
  • Joined: 04-March 12

Re: how can i test if a function is defined?

Posted 06 May 2018 - 07:09 AM

i need create functions, like virtual functions, but without re-prototype them on derived class and define them on Global Scope
like the events. but C++ don't have them and properties(but i did these class)


sepp2k:" I can't really offer more than that without knowing what your classes look like or why you feel you need macros to generate them in the first place."
in these case avoid me re-write some code.

This post has been edited by Cambalinho: 06 May 2018 - 07:11 AM

Was This Post Helpful? 0
  • +
  • -

#15 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6565
  • View blog
  • Posts: 22,528
  • Joined: 05-May 12

Re: how can i test if a function is defined?

Posted 06 May 2018 - 07:11 AM

Another bit of advice: In general, avoid using macros in C++. Most C++ programmers abhor macros.
Was This Post Helpful? 1
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »