2017-06-25 68 views
2

cppreference.com - std::optional标识std :: optional为自“C++ 17以来”可用。 C++ Standards Support in GCC - C++1z Language Features列出C++ 17的功能。我在列表中看不到std :: optional。是std ::可选为G ++记录?C++ 17 std :: G ++中可选?

#include <string> 
#include <iostream> 
#include <optional> 

// optional can be used as the return type of a factory that may fail 
std::optional<std::string> create(bool b) { 
    if(b) 
     return "Godzilla"; 
    else 
     return {}; 
} 

int main() 
{ 
    std::cout << "create(false) returned " 
       << create(false).value_or("empty") << '\n'; 

    // optional-returning factory functions are usable as conditions of while and if 
    if(auto str = create(true)) { 
     std::cout << "create(true) returned " << *str << '\n'; 
    } 
} 
+7

'optional'不是*语言功能*。这是C++ 17的库特性。所以它不会在语言功能部分列出。 –

回答