By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,911 Members | 1,192 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,911 IT Pros & Developers. It's quick & easy.

Per-type compile time constants?

P: n/a

Hi!
First of all, sorry for my English, it's not my native tongue.

Anyway, here we go:
suppose I create a class that handles my own custom text strings.
No, suppose I create TWO such classes, which differ e.g. in the
characters mappings and in the implementation, etc...

Necessary operators are overloaded, so that the following example
works as one may intuitively expect it to work:

MyFirstType MyFirstTypeInstanceA, MyFirstTypeInstanceB;
MySecondType MySecondTypeInstanceA,MySecondTypeInstanceB;

MyFirstTypeInstanceA=GetFirstTypeString();
MySecondTypeInstanceA=GetSecondTypeString();

MyFirstTypeInstanceB="Hello"+MyFirstTypeInstanceA+ NewLine;
MySecondTypeInstanceB="Hello"+MySecondTypeInstance A+NewLine;

Please focus on "NewLine". My problem is the following:

I want NewLine to be a *different* constant in the two cases
of above. The compiler knows, in the first case, that it is
evaluating an expression of type "MyFirstType" because it is
assigning the expression to an instance of that type. It also
knowns, in the second case, that this time it is evaluating
an expression of type MySecondType. So it has enough info to
understand that NewLine, if declared as constant, belongs to
its own class each time it evaluates a certain expression.
But I don't know if C++ allows this, hence this post of mine.

Sure, I could do something like:
MyFirstTypeInstanceA="Hello"+MyFirstType::NewLine;
MySecondTypeInstanceA="Hello"+MySecondType::NewLin e;

(I made them shorter to avoid the 78 chars Usenet limitation)

But it seems awkward to me to have to specify the type before
the constant, when e.g. the "Hello" conversion from char to
the right type is automaticly handled in an nice way by the
compiler.

Is there any way in C++ to make a (preferably compile time,
for performance reasons) constant depend on the "context" (I
mean on the type that is being evaluated in the expression)?

Sorry again for my English, it's not my native language.

Thanks a lot,
Mario

Aug 13 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
ma***@spamnotme.it wrote:
>
Hi!
First of all, sorry for my English, it's not my native tongue.

Anyway, here we go:
suppose I create a class that handles my own custom text strings.
No, suppose I create TWO such classes, which differ e.g. in the
characters mappings and in the implementation, etc...

Necessary operators are overloaded, so that the following example
works as one may intuitively expect it to work:

MyFirstType MyFirstTypeInstanceA, MyFirstTypeInstanceB;
MySecondType MySecondTypeInstanceA,MySecondTypeInstanceB;

MyFirstTypeInstanceA=GetFirstTypeString();
MySecondTypeInstanceA=GetSecondTypeString();

MyFirstTypeInstanceB="Hello"+MyFirstTypeInstanceA+ NewLine;
MySecondTypeInstanceB="Hello"+MySecondTypeInstance A+NewLine;

Please focus on "NewLine". My problem is the following:

I want NewLine to be a *different* constant in the two cases
of above. The compiler knows, in the first case, that it is
evaluating an expression of type "MyFirstType" because it is
assigning the expression to an instance of that type. It also
knowns, in the second case, that this time it is evaluating
an expression of type MySecondType. So it has enough info to
understand that NewLine, if declared as constant, belongs to
its own class each time it evaluates a certain expression.
But I don't know if C++ allows this, hence this post of mine.

Sure, I could do something like:
MyFirstTypeInstanceA="Hello"+MyFirstType::NewLine;
MySecondTypeInstanceA="Hello"+MySecondType::NewLin e;

(I made them shorter to avoid the 78 chars Usenet limitation)

But it seems awkward to me to have to specify the type before
the constant, when e.g. the "Hello" conversion from char to
the right type is automaticly handled in an nice way by the
compiler.

Is there any way in C++ to make a (preferably compile time,
for performance reasons) constant depend on the "context" (I
mean on the type that is being evaluated in the expression)?
[snip]

Maybe someting like this (although it maybe overly complicated):

#include <string>
#include <iostream>

struct a_string : private std::string {

a_string ( void )
{}

template < typename A >
a_string ( A const & a )
: std::string( a )
{}

template < typename A, typename B >
a_string ( A const & a, B const & b )
: std::string( a, b )
{}

a_string & operator+= ( a_string const & other ) {
std::string::operator+=( other );
return ( *this );
}

std::string const & str ( void ) const {
return ( *this );
}

};

struct b_string : private std::string {

b_string ( void )
{}

template < typename A >
b_string ( A const & a )
: std::string( a )
{}

template < typename A, typename B >
b_string ( A const & a, B const & b )
: std::string( a, b )
{}

b_string & operator+= ( b_string const & other ) {
std::string::operator+=( other );
return ( *this );
}

std::string const & str ( void ) const {
return ( *this );
}

};

struct new_line_const {

new_line_const ( int )
{}

operator a_string ( void ) const {
return ( a_string( "hello" ) );
}

operator b_string ( void ) const {
return ( b_string( "world" ) );
}

};

static
new_line_const const new_line = 0;

int main ( void ) {
a_string a ( "greetings: " );
a += new_line;
std::cout << a.str() << '\n';

b_string b ( "greetings: " );
b += new_line;
std::cout << b.str() << '\n';
}
Also note that the compiler will perform only one user defined conversion,
so this trick might not work in all circumstances.
Best

Kai-Uwe Bux
Aug 13 '06 #2

P: n/a

ma***@spamnotme.it skrev:
Hi!
First of all, sorry for my English, it's not my native tongue.

Anyway, here we go:
suppose I create a class that handles my own custom text strings.
No, suppose I create TWO such classes, which differ e.g. in the
characters mappings and in the implementation, etc...

Necessary operators are overloaded, so that the following example
works as one may intuitively expect it to work:

MyFirstType MyFirstTypeInstanceA, MyFirstTypeInstanceB;
MySecondType MySecondTypeInstanceA,MySecondTypeInstanceB;

MyFirstTypeInstanceA=GetFirstTypeString();
MySecondTypeInstanceA=GetSecondTypeString();

MyFirstTypeInstanceB="Hello"+MyFirstTypeInstanceA+ NewLine;
MySecondTypeInstanceB="Hello"+MySecondTypeInstance A+NewLine;

Please focus on "NewLine". My problem is the following:

I want NewLine to be a *different* constant in the two cases
of above. The compiler knows, in the first case, that it is
evaluating an expression of type "MyFirstType" because it is
assigning the expression to an instance of that type. It also
knowns, in the second case, that this time it is evaluating
an expression of type MySecondType. So it has enough info to
understand that NewLine, if declared as constant, belongs to
its own class each time it evaluates a certain expression.
But I don't know if C++ allows this, hence this post of mine.

Sure, I could do something like:
MyFirstTypeInstanceA="Hello"+MyFirstType::NewLine;
MySecondTypeInstanceA="Hello"+MySecondType::NewLin e;

(I made them shorter to avoid the 78 chars Usenet limitation)

But it seems awkward to me to have to specify the type before
the constant, when e.g. the "Hello" conversion from char to
the right type is automaticly handled in an nice way by the
compiler.

Is there any way in C++ to make a (preferably compile time,
for performance reasons) constant depend on the "context" (I
mean on the type that is being evaluated in the expression)?
What about something like this sketch:

class NewLine_t {};

NewLine_t NewLine;

template <typename T>
T opeator+(T const& t, NewLine_t) { return t + T::NewLine; }

/Peter

Aug 13 '06 #3

P: n/a
<ma***@spamnotme.itwrote in message
news:44***********************@reader1.news.tin.it
Hi!
First of all, sorry for my English, it's not my native tongue.

Anyway, here we go:
suppose I create a class that handles my own custom text strings.
No, suppose I create TWO such classes, which differ e.g. in the
characters mappings and in the implementation, etc...

Necessary operators are overloaded, so that the following example
works as one may intuitively expect it to work:

MyFirstType MyFirstTypeInstanceA, MyFirstTypeInstanceB;
MySecondType MySecondTypeInstanceA,MySecondTypeInstanceB;

MyFirstTypeInstanceA=GetFirstTypeString();
MySecondTypeInstanceA=GetSecondTypeString();

MyFirstTypeInstanceB="Hello"+MyFirstTypeInstanceA+ NewLine;
MySecondTypeInstanceB="Hello"+MySecondTypeInstance A+NewLine;

Please focus on "NewLine". My problem is the following:

I want NewLine to be a *different* constant in the two cases
of above. The compiler knows, in the first case, that it is
evaluating an expression of type "MyFirstType" because it is
assigning the expression to an instance of that type.
You are supposing that the left-hand side of the assignment should influence
how the right-hand side is evaluated. It doesn't work like that.
It also
knowns, in the second case, that this time it is evaluating
an expression of type MySecondType. So it has enough info to
understand that NewLine, if declared as constant, belongs to
its own class each time it evaluates a certain expression.
But I don't know if C++ allows this, hence this post of mine.

Kai-Uwe has given a very nice solution. The following fleshes out Peter's
suggestion, which involves overloading operator+

#include <string>
#include <iostream>
using namespace std;
struct NewLine
{};

NewLine new_line;

class stringA : public string
{
public:
static string nl;
stringA()
{}
stringA(const char * cstr) : string(cstr)
{}
stringA(const string & sstr) : string(sstr)
{}
};
string stringA::nl = "Anew_line";

class stringB : public string
{
public:
static string nl;
stringB()
{}
stringB(const char* cstr) : string(cstr)
{}
stringB(const string & sstr) : string(sstr)
{}
};
string stringB::nl = "Bnew_line";

template <typename T>
T operator+(T const& t, NewLine) { return t + T::nl; }
int main()
{
stringA a1("This is a stringA ");
stringB b1("This is a stringB ");
stringA a2;
stringB b2;

a2 = a1 + new_line;
b2 = b1 + new_line;

cout << a2 << endl;
cout << b2 << endl;

return 0;
}
--
John Carson
Aug 13 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.