isConst - Loki

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • skscpp

    isConst - Loki

    I copied this from several sources online. Why doesn't it work?

    // =============== =============== =============== =============== ====
    #include <iostream>

    using namespace std;

    namespace utl
    {
    template <typename T>
    class TypeTraits
    {
    private:
    template<class U>
    struct UnConst
    {
    typedef U Result;
    enum { isConst = false };
    };

    template<class U>
    struct UnConst<const U>
    {
    typedef U Result;
    enum { isConst = true };
    };

    public:

    enum { isConst = UnConst<T>::isC onst };
    };
    }

    class Object
    {
    public:
    Object(int a) { i = a; }
    int i;
    };

    int main()
    {
    Object test(10);
    const Object* t = &test;

    if ( utl::TypeTraits <const Object*>::isCon st )
    cout << "Hooray - it's a const :)\n";
    else
    cout << "It's NOT a const :(\n";

    if ( utl::TypeTraits <Object*>::isCo nst )
    cout << "it's a const :(\n";
    else
    cout << "Hooray - It's NOT a const :)\n";

    cout << endl;
    }

    // =============== =============== =============== =============== ====


    Here is the output:

    It's NOT a const :(
    Hooray - It's NOT a const :)
  • Andrey Tarasevich

    #2
    Re: isConst - Loki

    skscpp wrote:[color=blue]
    > I copied this from several sources online. Why doesn't it work?
    > ...
    > int main()
    > {
    > Object test(10);
    > const Object* t = &test;
    >
    > if ( utl::TypeTraits <const Object*>::isCon st )
    > cout << "Hooray - it's a const :)\n";
    > else
    > cout << "It's NOT a const :(\n";
    >
    > if ( utl::TypeTraits <Object*>::isCo nst )
    > cout << "it's a const :(\n";
    > else
    > cout << "Hooray - It's NOT a const :)\n";
    >
    > cout << endl;
    > }
    > ...
    > Here is the output:
    >
    > It's NOT a const :(
    > Hooray - It's NOT a const :)[/color]

    "Doesn't work"? Where? I don't seen any problems with it. Neither 'const
    Object*' nor 'Object*' is 'const' type so the output should be 'It's NOT
    a const' in both cases. That's exactly what you got.

    --
    Best regards,
    Andrey Tarasevich
    Brainbench C and C++ Programming MVP

    Comment

    • sks_cpp

      #3
      Re: isConst - Loki


      "Alf P. Steinbach" <alfps@start.no > wrote in message
      news:3f1ee287.5 48431281@News.C IS.DFN.DE...
      [color=blue]
      > If you consistently refrain from placing 'const' in front of the
      > type name, which is a special case syntax, then you'll probably not
      > get confused about this again.[/color]

      How is that a special case syntax? In other words, what is that special
      case?

      So I should always use:
      Object const * (pointer to constant object)
      OR
      Object * const (const pointer to object)




      Comment

      • Kevin Wan

        #4
        Re: isConst - Loki

        sksjava@hotmail .com (skscpp) wrote in message news:<3ab9ba14. 0307231129.1911 df32@posting.go ogle.com>...[color=blue]
        > I copied this from several sources online. Why doesn't it work?
        >
        > // =============== =============== =============== =============== ====
        > #include <iostream>
        >
        > using namespace std;
        >
        > namespace utl
        > {
        > template <typename T>
        > class TypeTraits
        > {
        > private:
        > template<class U>
        > struct UnConst
        > {
        > typedef U Result;
        > enum { isConst = false };
        > };
        >
        > template<class U>
        > struct UnConst<const U>
        > {
        > typedef U Result;
        > enum { isConst = true };
        > };
        >
        > public:
        >
        > enum { isConst = UnConst<T>::isC onst };
        > };
        > }
        >
        > class Object
        > {
        > public:
        > Object(int a) { i = a; }
        > int i;
        > };
        >
        > int main()
        > {
        > Object test(10);
        > const Object* t = &test;
        >
        > if ( utl::TypeTraits <const Object*>::isCon st )
        > cout << "Hooray - it's a const :)\n";
        > else
        > cout << "It's NOT a const :(\n";
        >
        > if ( utl::TypeTraits <Object*>::isCo nst )
        > cout << "it's a const :(\n";
        > else
        > cout << "Hooray - It's NOT a const :)\n";
        >
        > cout << endl;
        > }
        >
        > // =============== =============== =============== =============== ====
        >
        >
        > Here is the output:
        >
        > It's NOT a const :(
        > Hooray - It's NOT a const :)[/color]

        Try the following, you'll get a const!

        if ( utl::TypeTraits <Object * const>::isConst )
        cout << "Hooray - it's a const :)\n";
        else
        cout << "It's NOT a const :(\n";

        Comment

        • Shane Beasley

          #5
          Re: isConst - Loki

          "Victor Bazarov" <v.Abazarov@att Abi.com> wrote in message news:<xJGTa.131 566$Ph3.17100@s ccrnsc04>...
          [color=blue]
          > Special case is allowing 'const' before type:
          >
          > const int a;
          >
          > is allowed along with more proper
          >
          > int const a;[/color]

          "Proper" makes it sound like the latter is objectively better than the
          former. Where to put "const" is just as much a style issue as where to
          put curly braces, the most significant byte, the big end of the egg,
          etc.

          I still look at "int const a;" funny. There's nothing inherently wrong
          with it, I understand what it means, and I know that "right-constians"
          do it that way in an attempt to mimic the way C++ seems to interpret
          it (right-to-left). However, it's not my style, and it wouldn't
          improve my ability to read or write C++ or the compiler's ability to
          interpret and translate it.

          Beyond understanding the difference between "before *" and "after *",
          it really doesn't matter what order specifiers come in. Really. :)
          [color=blue]
          > That's one of the causes of confusion among those who haven't
          > got used to reading declarations.[/color]

          I think the confusion is almost entirely the result of C declarator
          syntax, described by Stroustrup as an "experiment that failed." Since
          we're stuck with what we've got, and a *lot* of code which takes
          advantage of what we've got, the only viable solution is to get used
          to reading it.

          - Shane

          Comment

          Working...