Thread: is_pointer<T> trait question.

  1. #1
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463

    is_pointer<T> trait question.

    I came upon this sample code which the author said that std::is_pointer<T>::value always evaluate to false. The author didn't explain why this doesn't work and I haven't been able to find an explainable after search the web.This code wouldn't compile either with the error "incompatible operand type 'int' and 'int*'; i think this maybe related to the 'false' evaluation of the std::is_pointer<T>::value.

    So what makes std::is_pointer<T>::value always evaluated to false in this case?

    Code:
    template <typename T>
    void foo_bad(const T& val) {
       std::cout << (std::is_pointer<T>::value ? *val : val)                     
                      << std::endl;
    }
    "All that we see or seem
    Is but a dream within a dream." - Poe

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The compilation error has nothing to do with is_pointer.

    The "test ? a : b" expression requires a and b to be of the same (or compatible) types. *val and val are not.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    I came upon this sample code which the author said that std::is_pointer<T>::value always evaluate to false.
    O_o

    Assuming you didn't copy the wrong code, the author is simply wrong.

    [Edit]
    The code is broken as grumpy explains.

    I'm saying that `std::is_pointer<T>::value' would not always be false when the code is otherwise corrected.
    [/Edit]

    Soma
    Last edited by phantomotap; 11-12-2014 at 10:18 PM.
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 08-25-2014, 05:41 PM
  2. Replies: 1
    Last Post: 03-23-2011, 09:00 AM
  3. *szString = things question/memory question
    By Jang in forum C Programming
    Replies: 3
    Last Post: 01-20-2011, 04:59 AM
  4. Newbish Question file reading question....
    By kas2002 in forum C Programming
    Replies: 23
    Last Post: 05-17-2007, 12:06 PM
  5. a trait you can;t change...
    By Commander in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 02-13-2003, 08:14 AM