Per-type compile time constants?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mario@spamnotme.it

    #1

    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 MyFirstTypeInst anceA, MyFirstTypeInst anceB;
    MySecondType MySecondTypeIns tanceA,MySecond TypeInstanceB;

    MyFirstTypeInst anceA=GetFirstT ypeString();
    MySecondTypeIns tanceA=GetSecon dTypeString();

    MyFirstTypeInst anceB="Hello"+M yFirstTypeInsta nceA+NewLine;
    MySecondTypeIns tanceB="Hello"+ MySecondTypeIns tanceA+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 "MyFirstTyp e" 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:
    MyFirstTypeInst anceA="Hello"+M yFirstType::New Line;
    MySecondTypeIns tanceA="Hello"+ MySecondType::N ewLine;

    (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

  • Kai-Uwe Bux

    #2
    Re: Per-type compile time constants?

    mario@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 MyFirstTypeInst anceA, MyFirstTypeInst anceB;
    MySecondType MySecondTypeIns tanceA,MySecond TypeInstanceB;
    >
    MyFirstTypeInst anceA=GetFirstT ypeString();
    MySecondTypeIns tanceA=GetSecon dTypeString();
    >
    MyFirstTypeInst anceB="Hello"+M yFirstTypeInsta nceA+NewLine;
    MySecondTypeIns tanceB="Hello"+ MySecondTypeIns tanceA+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 "MyFirstTyp e" 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:
    MyFirstTypeInst anceA="Hello"+M yFirstType::New Line;
    MySecondTypeIns tanceA="Hello"+ MySecondType::N ewLine;
    >
    (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::op erator+=( 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::op erator+=( 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

    Comment

    • peter koch

      #3
      Re: Per-type compile time constants?


      mario@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 MyFirstTypeInst anceA, MyFirstTypeInst anceB;
      MySecondType MySecondTypeIns tanceA,MySecond TypeInstanceB;
      >
      MyFirstTypeInst anceA=GetFirstT ypeString();
      MySecondTypeIns tanceA=GetSecon dTypeString();
      >
      MyFirstTypeInst anceB="Hello"+M yFirstTypeInsta nceA+NewLine;
      MySecondTypeIns tanceB="Hello"+ MySecondTypeIns tanceA+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 "MyFirstTyp e" 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:
      MyFirstTypeInst anceA="Hello"+M yFirstType::New Line;
      MySecondTypeIns tanceA="Hello"+ MySecondType::N ewLine;
      >
      (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

      Comment

      • John Carson

        #4
        Re: Per-type compile time constants?

        <mario@spamnotm e.itwrote in message
        news:44def7a0$0 $30234$4fafbaef @reader1.news.t in.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 MyFirstTypeInst anceA, MyFirstTypeInst anceB;
        MySecondType MySecondTypeIns tanceA,MySecond TypeInstanceB;
        >
        MyFirstTypeInst anceA=GetFirstT ypeString();
        MySecondTypeIns tanceA=GetSecon dTypeString();
        >
        MyFirstTypeInst anceB="Hello"+M yFirstTypeInsta nceA+NewLine;
        MySecondTypeIns tanceB="Hello"+ MySecondTypeIns tanceA+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 "MyFirstTyp e" 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


        Comment

        Working...