how can i test if a function is defined?

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

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

#31 Cambalinho   User is offline

  • D.I.C Addict

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

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

Posted 06 May 2018 - 02:16 PM

so it can be the 'catch' without 'exception'?
Was This Post Helpful? 0
  • +
  • -

#32 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6217
  • View blog
  • Posts: 21,462
  • Joined: 05-May 12

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

Posted 06 May 2018 - 02:17 PM

Your main() in post #29 doesn't seem to do anything.
Was This Post Helpful? 1
  • +
  • -

#33 Cambalinho   User is offline

  • D.I.C Addict

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

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

Posted 06 May 2018 - 02:19 PM

true, because the test() class constructor call it
Was This Post Helpful? 0
  • +
  • -

#34 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6217
  • View blog
  • Posts: 21,462
  • Joined: 05-May 12

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

Posted 06 May 2018 - 02:53 PM

I noticed that after I pasted in the code into Code::Blocks. I forgot that you also like to use globals and so I wasn't expecting your InstanceName to also be a global variable.

Why not use virtual functions and pure virtual functions the way they were meant to be used in the language?
#include <iostream>

class Base
{
public:
    virtual ~Base()
    {
    }

    virtual void Foo()
    {
        std::cout << "This is the Base::Foo()" << std::endl;
    }

    virtual void Bar()
    {
        std::cout << "This is the Base::Bar()" << std::endl;
    }

    virtual void Baz() = 0;
};

class A : public Base
{
public:
    void Foo() override;
    void Baz() override;
};

void A::Foo()
{
    std::cout << "This is the A::Foo()" << std::endl;
}

void A::Baz()
{
    std::cout << "This is the A::Baz()" << std::endl;
}

class B : public Base
{
public:
    void Foo() override;
    void Baz() override;
};

void B::Baz()
{
    std::cout << "This is the B::Foo()" << std::endl;
}

void CallBaseMethods(Base & B)/>/>/>
{
    b.Foo();
    b.Bar();
    b.Baz();
}

class C : public Base
{
};

#if NEVER
class D : public Base
{
public:
    void Baz() override;
};

// Forgetting to declare the override D::Foo() gives the following error:
//     error: no 'void D::Foo()' member function declared in class 'D'
void D::Foo()
{
    std::cout << "This is the D::Foo()" << std::endl;
}

void D::Baz()
{
    std::cout << "This is the D::Baz()" << std::endl;
}
#endif  // NEVER

int main()
{
    A a;
    CallBaseMethods(a);

    // Declaring B::Baz() override, but forgetting to define B::Baz() gives
    // the following gives the error:
    //     undefined reference to `vtable for B'
    // B b;
    // CallBaseMethods(B)/>/>/>;

    // Forgetting to override pure virtual Base::Baz() gives the following error:
    //     In function 'int main()':|
    //     error: cannot declare variable 'c' to be of abstract type 'C'|
    //     note:   because the following virtual functions are pure within 'C':|
    //     note:     virtual void Base::Baz()|
    // C c;
    // CallBaseMethods(c);

    return 0;
}



Output:
This is the A::Foo()
This is the Base::Bar()
This is the A::Baz()



What does supplying the fallback method within your test template provide that defining the basic implementation in the Base class does not provide?
Was This Post Helpful? 1
  • +
  • -

#35 Cambalinho   User is offline

  • D.I.C Addict

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

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

Posted 07 May 2018 - 12:21 PM

finally i have what i need see these 2 macros and the sample:
#include <iostream>

#if defined __GNUC__
#define EVENT [[gnu::weak]]
#elif defined __clang__
#define EVENT [[llvm::weak]]
#endif

#define IS_EVENT_DEFINED(sym) (static_cast<bool>(sym))

template <typename CBase>
class test
{
    CBase* atPointer = nullptr;
public:
    test(CBase *clsPointer) : atPointer(clsPointer)
    {
        if (IS_EVENT_DEFINED(&CBase::MouseClick)) //testing if the function is defined
            atPointer->MouseClick();


        if (IS_EVENT_DEFINED(&CBase::Move))
            atPointer->Move();

    }
    void call()
    {
        if (IS_EVENT_DEFINED(&CBase::MouseClick))
            atPointer->MouseClick();

        if (IS_EVENT_DEFINED(&CBase::Move))
            atPointer->Move();

    }
};

class InstanceName : public test <InstanceName>
{
public:
    InstanceName(): test(this){;}
    ~InstanceName(){;}
public:
    EVENT void MouseClick(); //creating the function using 'event' macro(is like a macro definition)
    EVENT void Move();
} InstanceName;

//yes.. we can comment these functions without problems
void InstanceName::MouseClick()
{
    std::cout << "Mouse click\n";
}


void InstanceName::Move()
{
   std::cout << "Move\n";
}


int main()
{
    InstanceName.call();
    std::cin.get();
    return 0;
}

thank you so much for all to all

This post has been edited by Cambalinho: 07 May 2018 - 12:22 PM

Was This Post Helpful? 0
  • +
  • -

#36 Cambalinho   User is offline

  • D.I.C Addict

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

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

Posted 07 May 2018 - 01:35 PM

i was testing on Visual Studio:
these line isn't compatible?
class InstanceName : public test <InstanceName> //template base initialization

Was This Post Helpful? 0
  • +
  • -

#37 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2620
  • View blog
  • Posts: 4,175
  • Joined: 21-June 11

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

Posted 07 May 2018 - 02:00 PM

View PostCambalinho, on 07 May 2018 - 09:21 PM, said:

#if defined __GNUC__
#define EVENT [[gnu::weak]]
#elif defined __clang__
#define EVENT [[llvm::weak]]
#endif



View PostCambalinho, on 07 May 2018 - 10:35 PM, said:

i was testing on Visual Studio


Why? When writing those above lines, you knew that this would not work on Visual Studio (or any other compiler that isn't gcc or clang), right? Or did you add another #elif in the meantime?
Was This Post Helpful? 1
  • +
  • -

#38 Cambalinho   User is offline

  • D.I.C Addict

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

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

Posted 07 May 2018 - 02:02 PM

for the moment, i'm asking these line:
class InstanceName : public test <InstanceName> //template base initialization

Was This Post Helpful? 0
  • +
  • -

#39 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2620
  • View blog
  • Posts: 4,175
  • Joined: 21-June 11

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

Posted 07 May 2018 - 02:15 PM

Can you clarify your question, please? There's nothing about that line that would be incompatible with Visual Studio.

In fact, your code compiles fine for me in Visual Studio if I remove the EVENTs from your class (though of course you'll get linker errors if you remove any of your definitions).

I'd still like an answer to my question though: What's the point of testing on VS when you're clearly not trying to support VS? Or did you try to add support for VS in the mean time? In that case, we'd probably need to see your new code to tell you why you're running into trouble with it.

This post has been edited by sepp2k: 07 May 2018 - 02:15 PM

Was This Post Helpful? 1
  • +
  • -

#40 Cambalinho   User is offline

  • D.I.C Addict

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

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

Posted 07 May 2018 - 02:22 PM

1 - now i add the elif for MVS, but i don't know the right macros:
#if defined __GNUC__
#define EVENT [[gnu::weak]]
#elif defined __clang__
#define EVENT [[llvm::weak]]
#elif define  _MSC_VER //these is purple... maybe is the  correct one
#define EVENT [[msvc ::weak]] //here i don't know
#endif

#define IS_EVENT_DEFINED(sym) (static_cast<bool>(sym))


2 - i will show you that line error:
class InstanceName : public test<InstanceName>

"variable 'InstanceName' isn't a type"(more or less translated from Portuguese to English)
Was This Post Helpful? 0
  • +
  • -

#41 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6217
  • View blog
  • Posts: 21,462
  • Joined: 05-May 12

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

Posted 07 May 2018 - 05:43 PM

Change your global variable name from InstanceName to something like GlobalInstanceName and see if the error goes away. You are likely confusing the compiler by having the class name and variable name being the same.

As far as I know, MSVC++ doesn't have anything explicitly like a weak symbols. The way it works is that the linker will give priority to a symbol found in a .OBJ over a symbol found in a .LIB. So it sort of works like the GCC and CLang weak symbol except for MSVC++ requires that you eventually have the symbol somewhere, unlike GCC or CLANG which will let you successfully link without he symbol.
Was This Post Helpful? 2
  • +
  • -

#42 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6217
  • View blog
  • Posts: 21,462
  • Joined: 05-May 12

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

Posted 07 May 2018 - 05:59 PM

As a quick aside, I didn't need to change the name of the global InstanceName in VS2017 to build successfully (after I defined EVENT to be nothing for MSVC). Here's my build output:
1>------ Build started: Project: SimpleCppConsole, Configuration: Debug Win32 ------
1>SimpleCppConsole.cpp
1>stdafx.cpp
1>Generating Code...
1>SimpleCppConsole.vcxproj -> D:\z\Test\SimpleCppConsole\Debug\SimpleCppConsole.exe
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========



That so-call "error" is from Intellisense. Do not rely too much on Intellisense. Intellisense does not do a full compile. It tries to do the best it can with a light weight parser and symbol engine. Granted that C++ Intellisense is now 10 times better than it used to be back in VS2008, it is still not perfect. Only trust a clean build and link.
Was This Post Helpful? 2
  • +
  • -

#43 Cambalinho   User is offline

  • D.I.C Addict

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

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

Posted 10 May 2018 - 07:34 AM

thank you so much.. after did the 'clean'(have learned now... thanks) that error isn't showed.
but i continue with another error:
int main()
{
	InstanceName.call();

error: E0040 esperado um identificador
"especting an ID"(more or less translated).
what these means?

This post has been edited by Cambalinho: 10 May 2018 - 07:35 AM

Was This Post Helpful? 0
  • +
  • -

#44 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6217
  • View blog
  • Posts: 21,462
  • Joined: 05-May 12

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

Posted 10 May 2018 - 02:07 PM

Post your updated code.
Was This Post Helpful? 0
  • +
  • -

#45 Cambalinho   User is offline

  • D.I.C Addict

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

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

Posted 10 May 2018 - 02:16 PM

after see too much errors, using normal code, i'm updating the VS2017.
tomorrow i will say something
Was This Post Helpful? 0
  • +
  • -

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