Address of static method

T

Thorsten Kiefer

Hi,
my compiler tells me that the address of a static method will always
evaluate to true (which is 1). Why that ? How can i get the address of a
static method ?
I'm using gcc3.

Regards
Thorsten
 
M

mlimber

Thorsten said:
Hi,
my compiler tells me that the address of a static method will always
evaluate to true (which is 1). Why that ? How can i get the address of a
static method ?
I'm using gcc3.

It should only evaluate to true if you are testing it. Otherwise it
should simply be a non-zero address. Please post a minimal but complete
sample of code that demonstrates the problem (cf.
http://parashift.com/c++-faq-lite/how-to-post.html#faq-5.8).

Cheers! --M
 
T

Thorsten Kiefer

mlimber said:
It should only evaluate to true if you are testing it. Otherwise it
should simply be a non-zero address. Please post a minimal but complete
sample of code that demonstrates the problem (cf.
http://parashift.com/c++-faq-lite/how-to-post.html#faq-5.8).

Cheers! --M

thread.hpp :
#include <pthread.h>
#include <iostream>


namespace std {

class Thread {
protected:
pthread_t pthread;

static void *start_routine(void *x);
static void test() {};

public:
Thread(){
cout << &start_routine << endl;
cout << &test << endl;
int r = pthread_create(&pthread,0,start_routine,this);
}
virtual int run() = 0;
};

}

threadtest.cpp :
#include <thread.hpp>
#include <iostream>


using namespace std;


class Thread1 : public Thread {
public:
int run() {
for(int i = 0;i < 10;++i)
cout << i << endl;
}
};


int main(int argc,char **argv){
Thread1 t1;
}


Result:
1
1
Segmentation fault


Greets
tk
 
M

mlimber

Thorsten said:
thread.hpp :
#include <pthread.h>
#include <iostream>


namespace std {

class Thread {
protected:
pthread_t pthread;

static void *start_routine(void *x);
static void test() {};

public:
Thread(){
cout << &start_routine << endl;
cout << &test << endl;
int r = pthread_create(&pthread,0,start_routine,this);
}
virtual int run() = 0;
};

}

threadtest.cpp :
#include <thread.hpp>
#include <iostream>


using namespace std;


class Thread1 : public Thread {
public:
int run() {
for(int i = 0;i < 10;++i)
cout << i << endl;
}
};


int main(int argc,char **argv){
Thread1 t1;
}


Result:
1
1
Segmentation fault


Greets
tk

This is neither a minimal nor a complete program -- the pthread
business is non-standard and unnecessary here to demonstrate your
problem, and you don't define Thread::start_routine() anywhere. Also,
you didn't tell us where the warning message was issued, but I checked,
and it's on each of the couts in Thread::Thread(). Before I get to
that, however, you may not add things to the std namespace, so get
Thread out of there. Anyway, it looks like that's a bug in g++ 3. It
doesn't happen on VC++ 6, 2003 (online), 2005, EDG (online) or Comeau
(online). Better ask in a gnu group. See this FAQ for some
possibilities:

http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.9

Cheers! --M
 
T

Thorsten Kiefer

Hi,
ok i prepared anither example:

test.cpp:
#include <iostream>

using namespace std;


struct Test {
static void f();
};

void Test::f(){
}

int main(int argc,char ** argv){
cout << &Test::f << endl;
}

compile :
g++ -O3 -funroll-loops -fomit-frame-pointer -march=athlon-xp -lstdc++
test.cpp -o test
test.cpp: In function ‘int main(int, char**)’:
test.cpp:14: warning: the address of ‘static void Test::f()’, will always
evaluate as ‘true’

output :
1


What's the ouput with VC++ ?

Regards
Thorsten
 
G

Greg

Thorsten said:
Hi,
ok i prepared anither example:

test.cpp:
#include <iostream>

using namespace std;


struct Test {
static void f();
};

void Test::f(){
}

int main(int argc,char ** argv){
cout << &Test::f << endl;
}

compile :
g++ -O3 -funroll-loops -fomit-frame-pointer -march=athlon-xp -lstdc++
test.cpp -o test
test.cpp: In function 'int main(int, char**)':
test.cpp:14: warning: the address of 'static void Test::f()', will always
evaluate as 'true'

You program is converting a non-null function pointer to a bool value
when it streams the function pointer's value to std::cout - so the
compiler is right: the value resulting from the conversion will always
be true.

To obtain the address of a global or static function, either use its
name alone or its name appended by the "address of" operator (&).

So either:

Test::f

or

&Test::f

specifies the address of the Test::f() static function.

Greg
 
P

Pete Becker

Thorsten said:
cout << &Test::f << endl;

There is no stream inserter for a pointer-to-member-function, so the
compiler converts &Test::f to a Boolean value and shows that. Since the
pointer isn't null, the value is true.
 
T

Thorsten Kiefer

Pete said:
There is no stream inserter for a pointer-to-member-function, so the
compiler converts &Test::f to a Boolean value and shows that. Since the
pointer isn't null, the value is true.
Hi,
thanks for your answers.
cout << (void *)&Test::f << endl;
works fine.

In my Thread-program i got a Segmentation fault, and I thought it's because
the address of the static member function is 1. But the reason is that i
forgot to link libpthread.so.

Greets
Thorsten
 
P

Pete Becker

Thorsten said:
thanks for your answers.
cout << (void *)&Test::f << endl;
works fine.

No, it doesn't. It just happens to give an answer that looks like it
makes sense. There is no requirement that a pointer to member function
(static or otherwise) be convertible to void*.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top