disable implicit type conversion

R

RezaRob

Is there any way to prevent implicit type conversion when parameter
passing.
For example between these types:
class Window * win1;
class BorderedWindow * win2;

Thanks in advance.

Reza.
 
K

Kai-Uwe Bux

RezaRob said:
Is there any way to prevent implicit type conversion when parameter
passing.
For example between these types:
class Window * win1;
class BorderedWindow * win2;

Assuming that BorderedWindow derives from Window, there are two possible
meanings to your question:

a) Disable conversion based upon dynamic type checking.

This is something that the function body will have to do. At compile time
the dynamic type is (treated as) unknown.


b) Disable conversion based upon the static parameter type.

There are options:

b1) You can trigger a linker error by overloading with an undefined
function:

class Base {};
class Derived : public Base {};

void function ( Base * b_ptr ) {}

// unimplemented:
void function ( Derived * d_ptr );

int main ( void ) {
Derived * b_ptr = new Derived;
function ( ptr );
}

b2) You can trigger a compiler error using templates:

struct yes_type { char dummy; };
struct no_type { yes_type a; yes_type b; };

template < bool b, typename T >
struct enable_if;

template < typename T >
struct enable_if<true,T> { typedef T type; };


template < bool b, typename T >
struct disable_if;

template < typename T >
struct disable_if<false,T> { typedef T type; };

template < typename S, typename T >
struct same_type {

typedef no_type type;
static const bool value = false;

}; // same_type<>

template < typename S >
struct same_type<S,S> {

typedef yes_type type;
static const bool value = true;

}; // same_type<>



class Base {};

class Derived : public Base {};

template < typename BasePtr >
void function ( BasePtr b_ptr,
typename enable_if< s
ame_type<BasePtr,Base*>::value, void* >::
type dummy = 0 ) {
}

This will, in fact, prevent _all_ conversions to Base* from any derived
type. If you want to disable specific conversions, you can vary the idea
using disable_if instead of enable_if.



Best

Kai-Uwe Bux
 
B

Barry

Kai-Uwe Bux said:
Assuming that BorderedWindow derives from Window, there are two possible
meanings to your question:

a) Disable conversion based upon dynamic type checking.

This is something that the function body will have to do. At compile time
the dynamic type is (treated as) unknown.


b) Disable conversion based upon the static parameter type.

There are options:

b1) You can trigger a linker error by overloading with an undefined
function:

class Base {};
class Derived : public Base {};

void function ( Base * b_ptr ) {}

// unimplemented:
void function ( Derived * d_ptr );

int main ( void ) {
Derived * b_ptr = new Derived;
function ( ptr );
}

b2) You can trigger a compiler error using templates:

struct yes_type { char dummy; };
struct no_type { yes_type a; yes_type b; };

template < bool b, typename T >
struct enable_if;

template < typename T >
struct enable_if<true,T> { typedef T type; };


template < bool b, typename T >
struct disable_if;

template < typename T >
struct disable_if<false,T> { typedef T type; };

template < typename S, typename T >
struct same_type {

typedef no_type type;
static const bool value = false;

}; // same_type<>

template < typename S >
struct same_type<S,S> {

typedef yes_type type;
static const bool value = true;

}; // same_type<>



class Base {};

class Derived : public Base {};

template < typename BasePtr >
void function ( BasePtr b_ptr,
typename enable_if< s
ame_type<BasePtr,Base*>::value, void* >::
type dummy = 0 ) {

type = 0

we don't even have to use a named placeholder.
 
R

RezaRob

b) Disable conversion based upon the static parameter type.

There are options:

b1) You can trigger a linker error by overloading with an undefined
function:
[...]
b2) You can trigger a compiler error using templates:

[...]

Thanks so much for giving thorough examples and explanations. In
hindsight, it would have been useful if I said this earlier: I'm
working with a C programmer who has strict correctness/consistency
standards to feel comfortable with the programming language. If I
show him a "trick" for protecting against unwanted conversions, he
won't buy it. Rather, if there was some type of "compiler switch" for
this, it would be much more attractive; or, a more powerful keyword
similar to "explicit".

But from your explanations, I think a simple keyword looks out of the
question.

Thank you Kai-Uwe :)

Reza.
 

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,201
Messages
2,571,048
Members
47,647
Latest member
NelleMacy9

Latest Threads

Top