Solved

typename T - statement return Element < T()

Posted on 2004-10-18
9
280 Views
Last Modified: 2013-12-14

Question with regard to the typename T in this statement  'return Elem < T()'.  The typename per the example is 'int' hence the return statement equates to "return Elem < int()".    For Elem 3, we have " return 3 < int() " but I'm confused on what the default construction call for the built in type - in this case - int() equates to?   ie.  "return 3 < ???"


# include <iostream>
# include <string>
# include <iterator>
# include <algorithm>
# include <sstream>
# include <iomanip>
# include <vector>

using namespace std;

// [Predicate] object.
struct RemoveLessThanZero
 {
  template<typename T>
  bool operator()( const T& Elem ) const
    {
     return Elem < T();
    }
 };

int main()
 {
  int Array[] = { 3, 2, 1, 0, -1, -2, -3, -4 };

  // Calculate the size of the array.
  static const std::size_t
         NrElems( sizeof Array / sizeof* Array );

  // Construct the vector with the 'Array' elements.
  std::vector<int> V( Array, Array + NrElems );

  // Print the contents of the vector to stdout.
  std::copy( V.begin(), V.end(),
             std::ostream_iterator<int>( std::cout, " " ) );

  // Remove all negative values from the vector, with
  // the help of our [Predicate] object.
  V.erase( std::remove_if( V.begin(), V.end(),
                           RemoveLessThanZero() ), V.end() );

  // Sort the vector elements prior to printing.
  std::sort( V.begin(), V.end() );
  std::cout << std::endl;

  // Again, print the contents of the vector to stdout.
  std::copy( V.begin(), V.end(),
             std::ostream_iterator<int>( std::cout, " " ) );

  return 0;
 }
0
Comment
Question by:forums_mp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 30 total points
ID: 12339317
I guess int() calls int constructor which creates int instance and sets it to 0. 3 < int() is like 3 < 0.

Line
i = int();

looks in Disassembly window by the following way:

22:       i = int();
004114DF   mov         dword ptr [ebp-4],0

This means, i = 0.

Other test:

class A
{
public:
    A()
    {
        cout << "A" << endl;
    }
};

    A a;
    a = A();

Result is:
A
A

0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 12339359
>>>> return Elem < int()".    

That statement compiles but doesn't work for build-in types. So, i would call it a bug. T() creates a new int in your case, that isn't initialized --> the result is undefined.

To make it work, you have to define a member variable of type T that takes an 'empty' or 'zero' instance of type T. You can pass the empty value by the constructor or - more usual - by providing a separate init or set function. The statement above then turns to

   return Elem < m_empty;

Regards, Alex

0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 12339406
>>>> I guess int() calls int constructor which creates int instance and sets it to 0

No, only VC Debugger initializes 'new' instances with zeroes. There is no 'int' constructor in C++.

Regards, Alex

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:forums_mp
ID: 12340283

>> So, i would call it a bug.
A bug considering the output made sense on repeated runs ..

>>> To make it work, you have to define a member variable of type T that takes an 'empty' or 'zero' instance of type T. You can pass the empty value by the constructor or - more usual - by providing a separate init or set function. The statement above then turns to

For instance.
0
 
LVL 15

Assisted Solution

by:efn
efn earned 20 total points
ID: 12344159
It's deja vu all over again...

http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_21031400.html

Summarizing:  A plain old data object whose initializer is an empty set of parentheses should be zero-initialized, as the first Alex said.  However, there are compilers that don't do this right.

--efn
0
 

Author Comment

by:forums_mp
ID: 12346422

efn

The issue is not default initilization.  I understand that.  I realized though that I've interpreted the call to  V.erase/remove_if incorrectly.   The comparision is
   3 < 0
   2 < 0
   :
   :
  -4 < 0

For some reason I thought (momentary insanity i suppose :)) about an element by element comparison and was trying to figure out how T() fit in..
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 12346823
>> No, only VC Debugger initializes 'new' instances with zeroes.

Debugger ??
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 12347258
>> Debugger ??

I meant VC Built in 'Debug Configuration'
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 12397915
Program built by Microsoft VC++ compiler in Debug Configuration.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question