J
josef angermeier
i wrote a display driver for a lcd segment display which doesnt recognize
ascii character so that each character of an output string needs to be
converted byte by byte by looking in a const character table. in short,
when calling my output function display.output("hallo") i dont want to call
the output function at runtime with the ascii character string but with
display.output(CONVERTED("hallo")). ok therefor i could maybe write a macro
with a loop (must be difficult), but while reading bjarne stroustrup c++ i
just thought i could use implicit type conversion and maybe g++ optimizes
the conversion away.
i wrote a test class for this, but g++ doesnt optimize the conversion loop
away:
static const char table[] = {'j','o','s','e','f'};
class DisplayData{
public:
DisplayData(const char* str) : ptr(str) {
for(int i=0;i<5;i++){
const_cast<char*>(str) = table[const_cast<char*>(str)]; // each
byte is converted in another one
}
}
const char* ptr;
};
void output(const DisplayData& d){
cout << d.ptr << endl;
}
int main(){
const char str[] = "\00\01\02\03\04"; // the original character string
which should be optimized away
output(str);
}
any idea to get it optimized or another way to solve this problem ?
josef
ascii character so that each character of an output string needs to be
converted byte by byte by looking in a const character table. in short,
when calling my output function display.output("hallo") i dont want to call
the output function at runtime with the ascii character string but with
display.output(CONVERTED("hallo")). ok therefor i could maybe write a macro
with a loop (must be difficult), but while reading bjarne stroustrup c++ i
just thought i could use implicit type conversion and maybe g++ optimizes
the conversion away.
i wrote a test class for this, but g++ doesnt optimize the conversion loop
away:
static const char table[] = {'j','o','s','e','f'};
class DisplayData{
public:
DisplayData(const char* str) : ptr(str) {
for(int i=0;i<5;i++){
const_cast<char*>(str) = table[const_cast<char*>(str)]; // each
byte is converted in another one
}
}
const char* ptr;
};
void output(const DisplayData& d){
cout << d.ptr << endl;
}
int main(){
const char str[] = "\00\01\02\03\04"; // the original character string
which should be optimized away
output(str);
}
any idea to get it optimized or another way to solve this problem ?
josef