Search code examples
c++gccc++20c++-chronolibstdc++

C++20 <chrono> incomplete type used in nested name specifier


Now that GCC 11, which supports the new calendar and time zone features is released, I am trying to use these in my code.

With the following minimal example:

#include <chrono>
#include <iostream>

int main()
{
    using namespace std;

    cout << chrono::utc_clock::now() << endl;

    return EXIT_SUCCESS;
}

compiled using the following:

> /usr/local/gcc-11.1.0/bin/g++-11.1 -std=c++-20 main.cpp

gives the following error:

main.cpp: In function ‘int main()’:
main.cpp:8:36: error: incomplete type ‘std::chrono::utc_clock’ used in 
    nested name specifier
    8 |         cout << chrono::utc_clock::now() << endl;
      | 

I have looked into this error and it seems to usually stem from forward declarations. Does anyone have any ideas why this is happening?

I have also tried explicitly linking against the newly-built GCC 11 version of libstdc++.a:

/usr/local/gcc-11.1.0/bin/g++-11.1 -std=c++20 -L/usr/local/gcc-11.1.0/lib -lstdc++ main.cpp

But I get the same error message.

Some extra information:

> /usr/local/gcc-11.1.0/bin/g++-11.1 --version

gives the following output:

g++-11.1 (GCC) 12.0.0 20210427 (experimental)
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

which is unexpected to me since I'm expecting to see 11.1. I built this on MacOS and my clang++ version is 12.0.5. Is this expected and could this be a potential source of the problem?

Also, this website that shows compiler support for C++20 standard library features shows that GCC 11 has partial support for the updated chrono.

Lastly, I built GCC 11 from source using this video as a reference.


Solution

  • Only partial C++20 chrono support is in gcc at this time. Here is the current state of the libstdc++ source code on this matter. This shows a simple forward declaration of utc_clock, and no definition, which is consistent with the error message you are seeing.

    I am seeing more support for file_clock, including a low-level API to convert between file_clock::time_point and system_clock::time_point. This is a significant addition.

    Additionally I'm seeing support for the calendrical types in C++20 (e.g. year_month_day, etc.), which is what the release notes refers to.