473,320 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Per-type compile time constants?


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
3 1545
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

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
<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Greg Ferris | last post by:
I have read a number of posts with techniques for limiting the max number of characters entered into a textarea, but I'm looking for some advice on how to limit the number of rows and the number of...
4
by: Guadala Harry | last post by:
Is there any way for one Session to remove and update objects in another Session? I seriously doubt it, but thought I'd ask. Here's why: I have some data that is unique per user (or per session -...
7
by: Rob Nicholson | last post by:
We're using a well known presentation layer library to implement complex controls on an intranet site. IE has the following limitation which effectively means that you can only have 30 <STYLE> tags...
2
by: needin4mation | last post by:
Hi, I have to decide between a per device and a per license issue. We have several web services that have functions that use things like LOGON_USER. If I have a per device license, does that mean...
7
by: Randy Yates | last post by:
I'm a complete newbie to postgres so please look the other way if these questions are really stupid. Is it legitimate to have one database per data file? For organizational and backup purposes,...
12
by: bruno at modulix | last post by:
Hi I'm currently playing with some (possibly weird...) code, and I'd have a use for per-instance descriptors, ie (dummy code): class DummyDescriptor(object): def __get__(self, obj,...
32
by: Matias Jansson | last post by:
I come from a background of Java and C# where it is common practise to have one class per file in the file/project structure. As I have understood it, it is more common practice to have many...
10
by: Joseph Geretz | last post by:
I need to calculate miles per degree longitude, which obviously depends on latitude since lines of longitude converge at the poles. Via Google, I've come up with the following calculation: ...
9
by: bakxchaixDD | last post by:
I DON'T GET THIS project: Many treadmills output the speed in miles per hour. However, most runners think of their pace in minutes and seconds per mile. Write a program that inputs a decimal...
0
by: raveekumarg | last post by:
hi what is the difference between per seat , per server , per processor and i am new to sql 2000 , pl do explain the same.
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.