Confirm Template if correct

Discussion in 'C++' started by Nephi Immortal, Mar 3, 2011.

  1. Please confirm if I am correct.

    If you assign “unsigned char” to typename T, then T data is
    translated to unsigned char data as passing copy value.
    If you assign “unsigned char &” to typename T, then T data is
    translated to unsigned char &data as passing reference.

    template< typename L, typename R >
    class X {
    public:
    X< L, R >( L data ) : m_data( data ) {
    }

    X< L, R > &operator=( R data ) {
    m_data = data;
    return *this;
    }

    L m_data;
    };

    typedef unsigned char size_8;

    int main () {
    size_8 data = 0x34U;

    const X< size_8, size_8 > A( 0x12U ); // constant
    X< size_8 &, size_8 > B( data ); // reference

    A.m_data++; // error cannot modify left value because of constant
    class
    B.m_data++; // can modify non-constant class

    return 0;
    }
    Nephi Immortal, Mar 3, 2011
    #1
    1. Advertising

  2. On 3/2/2011 7:04 PM, Nephi Immortal wrote:
    > Please confirm if I am correct.
    >
    > If you assign “unsigned char” to typename T, then T data is
    > translated to unsigned char data as passing copy value.
    > If you assign “unsigned char&” to typename T, then T data is
    > translated to unsigned char&data as passing reference.


    First off, there is no 'T' in your code. If you're talking about a
    template like this one:

    template<class T> class TT { T data; };

    then yes, the member 'data' has the type 'unsigned char' if you
    instantiate 'TT' with 'unsigned char' and has the type 'unsigned char&'
    if you instantiate 'TT' with 'unsigned char&'. Is that what you need to
    confirm?

    >
    > template< typename L, typename R>
    > class X {
    > public:
    > X< L, R>( L data ) : m_data( data ) {
    > }
    >
    > X< L, R> &operator=( R data ) {
    > m_data = data;
    > return *this;
    > }
    >
    > L m_data;
    > };
    >
    > typedef unsigned char size_8;
    >
    > int main () {
    > size_8 data = 0x34U;
    >
    > const X< size_8, size_8> A( 0x12U ); // constant
    > X< size_8&, size_8> B( data ); // reference
    >
    > A.m_data++; // error cannot modify left value because of constant
    > class


    The 'A.m_data' has a value type. Since 'A' is const, 'A.m_data' value
    is also const.

    > B.m_data++; // can modify non-constant class


    The 'B.m_data' is a reference type (referencing a non-const object of
    type 'unsigned char'. Even if you define 'B' as const, you should be
    able to modify the object to which 'B.m_data' refers. Try declaring 'B'
    const.

    >
    > return 0;
    > }


    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Mar 3, 2011
    #2
    1. Advertising

  3. On Mar 3, 7:47 am, Victor Bazarov <> wrote:
    > On 3/2/2011 7:04 PM, Nephi Immortal wrote:
    >
    > >    Please confirm if I am correct.

    >
    > >    If you assign unsigned char to typename T, then T data is
    > > translated to unsigned char data as passing copy value.
    > >    If you assign unsigned char& to typename T, then T data is
    > > translated to unsigned char&data as passing reference.

    >
    > First off, there is no 'T' in your code.  If you're talking about a
    > template like this one:
    >
    >     template<class T> class TT { T data; };
    >
    > then yes, the member 'data' has the type 'unsigned char' if you
    > instantiate 'TT' with 'unsigned char' and has the type 'unsigned char&'
    > if you instantiate 'TT' with 'unsigned char&'.  Is that what you need to
    > confirm?
    >
    >
    >
    >
    >
    >
    >
    > > template<  typename L, typename R>
    > > class X {
    > > public:
    > >    X<  L, R>( L data ) : m_data( data ) {
    > >    }

    >
    > >    X<  L, R>  &operator=( R data ) {
    > >            m_data = data;
    > >            return *this;
    > >    }

    >
    > >    L m_data;
    > > };

    >
    > > typedef unsigned char size_8;

    >
    > > int main () {
    > >    size_8 data = 0x34U;

    >
    > >    const X<  size_8, size_8>  A( 0x12U ); // constant
    > >    X<  size_8&, size_8>  B( data ); // reference

    >
    > >    A.m_data++; // error cannot modify left value because of constant
    > > class

    >
    > The 'A.m_data' has a value type.  Since 'A' is const, 'A.m_data' value
    > is also const.
    >
    > >    B.m_data++; // can modify non-constant class

    >
    > The 'B.m_data' is a reference type (referencing a non-const object of
    > type 'unsigned char'.  Even if you define 'B' as const, you should be
    > able to modify the object to which 'B.m_data' refers.  Try declaring 'B'
    > const.
    >
    >
    >
    > >    return 0;
    > > }


    Yes, you are correct. I am trying to say. Can L type be replaced to
    either unsigned char or unsigned char&.

    For example:
    template< typename L >
    class X {
    ….
    L &m_data;
    }

    Then, declare const as const X< unsigned char (without &) > B and
    attempt to modify B.m_data should always generate an error message.

    You decide to remove & from between L and m_data like below.

    template< typename L >
    class X {
    ….
    L m_data;
    }

    and declare const X< unsigned char > A and non-const X< unsigned char
    &> B.

    I am surprised why C++ Compiler is able to compile without an error
    message if you declare const X< unsigned char &> B. Please explain
    the reason.
    Nephi Immortal, Mar 3, 2011
    #3
  4. On 3/3/2011 4:41 PM, Nephi Immortal wrote:
    > On Mar 3, 7:47 am, Victor Bazarov<> wrote:
    >> On 3/2/2011 7:04 PM, Nephi Immortal wrote:
    >>
    >>> Please confirm if I am correct.

    >>
    >>> If you assign unsigned char to typename T, then T data is
    >>> translated to unsigned char data as passing copy value.
    >>> If you assign unsigned char& to typename T, then T data is
    >>> translated to unsigned char&data as passing reference.

    >>
    >> First off, there is no 'T' in your code. If you're talking about a
    >> template like this one:
    >>
    >> template<class T> class TT { T data; };
    >>
    >> then yes, the member 'data' has the type 'unsigned char' if you
    >> instantiate 'TT' with 'unsigned char' and has the type 'unsigned char&'
    >> if you instantiate 'TT' with 'unsigned char&'. Is that what you need to
    >> confirm?
    >>
    >>
    >>
    >>
    >>
    >>
    >>
    >>> template< typename L, typename R>
    >>> class X {
    >>> public:
    >>> X< L, R>( L data ) : m_data( data ) {
    >>> }

    >>
    >>> X< L, R> &operator=( R data ) {
    >>> m_data = data;
    >>> return *this;
    >>> }

    >>
    >>> L m_data;
    >>> };

    >>
    >>> typedef unsigned char size_8;

    >>
    >>> int main () {
    >>> size_8 data = 0x34U;

    >>
    >>> const X< size_8, size_8> A( 0x12U ); // constant
    >>> X< size_8&, size_8> B( data ); // reference

    >>
    >>> A.m_data++; // error cannot modify left value because of constant
    >>> class

    >>
    >> The 'A.m_data' has a value type. Since 'A' is const, 'A.m_data' value
    >> is also const.
    >>
    >>> B.m_data++; // can modify non-constant class

    >>
    >> The 'B.m_data' is a reference type (referencing a non-const object of
    >> type 'unsigned char'. Even if you define 'B' as const, you should be
    >> able to modify the object to which 'B.m_data' refers. Try declaring 'B'
    >> const.
    >>
    >>
    >>
    >>> return 0;
    >>> }

    >
    > Yes, you are correct. I am trying to say. Can L type be replaced to
    > either unsigned char or unsigned char&.
    >
    > For example:
    > template< typename L>
    > class X {
    > ….
    > L&m_data;
    > }
    >
    > Then, declare const as const X< unsigned char (without&)> B and
    > attempt to modify B.m_data should always generate an error message.
    >
    > You decide to remove& from between L and m_data like below.
    >
    > template< typename L>
    > class X {
    > ….
    > L m_data;
    > }
    >
    > and declare const X< unsigned char> A and non-const X< unsigned char
    > &> B.
    >
    > I am surprised why C++ Compiler is able to compile without an error
    > message if you declare const X< unsigned char&> B. Please explain
    > the reason.


    I thought I did. The part of the constant object X<uc&> (the reference)
    is constant, but the object it refers to (the actual unsigned char) is
    not. I am not sure how to explain it to you better. Maybe this example
    (quiz) is going to help:

    #include <iostream>

    struct A {
    void foo() const { std::cout << "const foo\n"; }
    void foo() { std::cout << "regular foo\n"; }
    }

    struct B {
    A* pA;
    };

    int main() {
    A a; // non-const object of type A
    a.foo(); // expect "regular foo"
    B b = { &a }; // non-const B
    b.pA->foo(); // expect "regular foo"
    B const bc = { &a };
    bc.pA->foo(); // which one should you expect?
    }

    Try to answer the last question without running the program and explain
    your reasoning.

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Mar 4, 2011
    #4
  5. Nephi Immortal

    itaj sherman Guest

    On Mar 3, 11:41 pm, Nephi Immortal <> wrote:
    > On Mar 3, 7:47 am, Victor Bazarov <> wrote:
    >


    >
    > I am surprised why C++ Compiler is able to compile without an error
    > message if you declare const X< unsigned char &> B. Please explain
    > the reason.- Hide quoted text -
    >


    Consider that a reference to T is equivalent to a const pointer to T
    (non-null)
    T& eqv T* const

    Non-null const pointer has the same semantic as references, just
    different syntax.

    If you refrase you question using pointer syntax, would you still be
    surprised?
    If T is non-const, you change it through any pointer to T, even a
    const pointer to T.

    typedef unsigned char size_8;

    int main () {
    size_8 data = 0x34U;


    const X< size_8, size_8 > A( 0x12U );
    X< size_8* const, size_8 > B( &data ); //


    A.m_data++;
    (*(B.m_data))++; // B.m_data is a const pointer to non-const
    size_8


    return 0;
    }

    itaj
    itaj sherman, Mar 4, 2011
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Patrick Delifer

    JS Confirm dialog from datagrid template column

    Patrick Delifer, Apr 27, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    688
    Patrick Delifer
    Apr 28, 2004
  2. joon
    Replies:
    1
    Views:
    515
    Roedy Green
    Jul 8, 2003
  3. Dan

    correct or not correct?

    Dan, Oct 2, 2003, in forum: HTML
    Replies:
    7
    Views:
    439
  4. J.Ram
    Replies:
    7
    Views:
    649
  5. froil
    Replies:
    12
    Views:
    309
    Gunnar Hjalmarsson
    Mar 2, 2006
Loading...

Share This Page