Thread: STL problems

  1. #1
    Registered User
    Join Date
    Dec 2019
    Posts
    13

    STL problems

    I noticed that changing IDE some STL functions may be or may not be accepted as a member of a given library.

    For example I can use min() and max() just including <cmath>, but if I change compiler I must include <algorithm> too.

    This is not a problem for me, but it literally destroys portability (I tried the code on the same pc, so I can't imagine what could happen on anothe operating system)

    What can I do to be guaranteed that a given function can always be read by the compiler?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,402
    Quote Originally Posted by Foxel
    For example I can use min() and max() just including <cmath>, but if I change compiler I must include <algorithm> too.

    This is not a problem for me, but it literally destroys portability (I tried the code on the same pc, so I can't imagine what could happen on anothe operating system)
    From what I see, the C standard does not include min and max in <math.h>, hence there has been no version of the C++ standard that included std::min and std::max in <cmath>. Hence, it is likely that you were relying on an indirect inclusion of min and max (i.e., you included another header that happens to have included a header in which min and max as given in <algorithm> was declared), and so your code is not portable, as you discovered.

    Quote Originally Posted by Foxel
    What can I do to be guaranteed that a given function can always be read by the compiler?
    Check that the function is indeed provided for by the C++ standard, and is available by including a header that you included. This is best done with a copy of the C++ standard itself, but you could always use a draft version of the standard that is freely available online, or an online reference like cppreference.com and you'll more or less have the information you want.

    You can also try to detect non-standard stuff by compiling your code at high warning levels and explicitly specifying the version of the C standard that you want, though this doesn't guarantee that an indirect inclusion that cannot be relied upon will not happen.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    782
    Always include the proper header as specified in your reference (such as cppreference.com). The proper header is, and as far as I know has always been, <algorithm>. Always compile using a specific standard and high warning level. These days you should be using C++11 at a bare minimum, better still C++14, and preferably C++17.

    I think you are just assuming that min() was in <cmath>. It could've been defined inside <iostream>. For example, with g++ -std=c++11 -Wall -W -pedantic this compiles without warnings:
    Code:
    #include <iostream>
     
    int main() {
        int a = 1, b = 2;
        std::cout << std::min(a, b) << '\n';
        std::cout << std::max(a, b) << '\n';
    }
    <iostream> is defined in the standard to include <ios> (which includes <iosfwd>), <streambuf>, <istream>, and <ostream>, as well as defining i/o objects like cout and cin. Apparently at least one of those headers in the g++ implementation that I used either includes <algorithm> (which seems unlikely) or simply defines min() and max() for its own use (presumably inline so there's no issue with multiple definitions).

    But you cannot rely on that and should therefore always include the proper header.
    Last edited by john.c; 1 Week Ago at 05:59 PM.
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

  4. #4
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,674
    Quote Originally Posted by john.c View Post
    <iostream> is defined in the standard to include <ios> (which includes <iosfwd>), <streambuf>, <istream>, and <ostream>, as well as defining i/o objects like cout and cin. Apparently at least one of those headers in the g++ implementation that I used either includes <algorithm> (which seems unlikely) or simply defines min() and max() for its own use (presumably inline so there's no issue with multiple definitions).

    But you cannot rely on that and should therefore always include the proper header.
    For my installation of g++ it's getting included by /usr/include/c++/9/bits/char_traits.h (which is #including stl_algobase.h which is where max/min are for my installation; <algorithm> #includes stl_algobase.h. (Ultimately it's getting included because <ios> is including <exception> which leads to char_traits.h being included, leading to std_algobase.h inclusion)

    g++ -M test.cpp
    g++ -H test.cpp

    Edit: I added the above just for curiosity's sake. Of course you're correct in saying that you should always include the correct file, and std::min and std::max have been in algorithm since c++98
    Last edited by Hodor; 1 Week Ago at 06:45 PM.

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    782
    So it's including part of algorithm but not the whole thing. Interestingly it doesn't define std::minmax. It's in stl_algo.h. But that's all implementation details since we of course wouldn't directly include either of those headers.
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fread problems or memory problems
    By Lechuza in forum C Programming
    Replies: 1
    Last Post: 03-22-2009, 12:45 PM
  2. pow(x, y); problems
    By rhouli67 in forum C Programming
    Replies: 14
    Last Post: 02-17-2009, 10:51 AM
  3. Problems
    By nikko in forum C++ Programming
    Replies: 9
    Last Post: 04-20-2006, 06:17 AM
  4. Linking problems, class problems
    By Kheila in forum C++ Programming
    Replies: 12
    Last Post: 11-22-2005, 01:47 AM

Tags for this Thread