Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++14 boost::hana::is#u valid无法使用gcc8(及更多)和--std=c++;14_C++14_Language Lawyer_Template Meta Programming_Boost Hana_Gcc7 - Fatal编程技术网

C++14 boost::hana::is#u valid无法使用gcc8(及更多)和--std=c++;14

C++14 boost::hana::is#u valid无法使用gcc8(及更多)和--std=c++;14,c++14,language-lawyer,template-meta-programming,boost-hana,gcc7,C++14,Language Lawyer,Template Meta Programming,Boost Hana,Gcc7,我将此代码与std=c++14和gcc7.3一起使用: #include <iostream> #include <string> #include <type_traits> #include <boost/hana/assert.hpp> #include <boost/hana/equal.hpp> #include <boost/hana/type.hpp> namespace hana = boost::hana;

我将此代码与
std=c++14
gcc7.3
一起使用:

#include <iostream>
#include <string>
#include <type_traits>
#include <boost/hana/assert.hpp>
#include <boost/hana/equal.hpp>
#include <boost/hana/type.hpp>
namespace hana = boost::hana;

template<class T>
bool foo(T elem)
{
    constexpr auto has_overload_to_string = hana::is_valid([](auto t) -> decltype(to_string(t)) {});
    constexpr bool hasOverloadTo_string = has_overload_to_string(elem);
    return hasOverloadTo_string;
}

int main()
{ 
    std::string elem;
    std::cin >> elem;
    foo(elem);
}  
#包括
#包括
#包括
#包括
#包括
#包括
名称空间hana=boost::hana;
模板
布尔福(T元素)
{
constexpr auto具有_重载_to _string=hana::是否有效([](auto t)->decltype(to _string(t)){});
constexpr bool hasOverloadTo_string=has_OverloadTo_string(元素);
返回到_字符串;
}
int main()
{ 
字符串元素;
标准::cin>>元素;
foo(elem);
}  
而且效果很好:

如果现在我使用gcc10.1,则会出现以下错误:

prog.cc:在“bool foo(T)[带T=std::\uuuucxx11::basic\ustring]的实例化中”:
进度cc:41:13:此处需要
prog.cc:27:38:错误:在常量表达式中为非文字类型“foo::”的临时
27 |[[可能未使用]]constexpr auto具有到字符串的重载=
|                                      ^~~~~~~~~~~~~~~~~~~~~~
prog.cc:28:21:注意:“foo::”不是文字,因为:
28 | hana::is_valid([](自动t)->decltype(to_字符串(t)){});
|                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cc1plus:注意:“foo::”是一种闭包类型,在C++17及更高版本中仅为文本


我的问题是:gcc7.3对C++14是否过于宽容,而
是否有效
在不应该的情况下工作,或者gcc8和更多版本在C++14中添加了一个bug?

错误与
hana::is_有效
无关,但lambda在C++14的常量表达式中无效

这里已经有一个很好的语言律师回答:

Clang还始终提供一个错误,所以很明显,以前的gcc版本在允许这一点上是不正确的


要解决此问题,只需删除变量声明中的
constepr
限定符。

此错误与
hana::is_valid
无关,但lambda在C++14中的常量表达式中无效

这里已经有一个很好的语言律师回答:

Clang还始终提供一个错误,所以很明显,以前的gcc版本在允许这一点上是不正确的


要解决这个问题,只需删除变量声明中的
constepr
限定符。

我不知道wandbox是如何管理它的,但在godbolt上,它适用于所有版本。C++17放宽了对lambdas的限制,这样它就可以再次编译了。Thx的信息我不知道wandbox如何管理这一点,但在godbolt它FAL的所有版本。C++17放宽了对lambdas的限制,这样它就可以再次编译了。谢谢,我错过了一个事实,我可以在constexpr声明中使用非constexpr lambda!它工作得很好:谢谢,我错过了一个事实,我可以在constexpr声明中使用非constexpr lambda!它运行良好:
prog.cc: In instantiation of 'bool foo(T) [with T = std::__cxx11::basic_string<char>]':
prog.cc:41:13:   required from here
prog.cc:27:38: error: temporary of non-literal type 'foo<std::__cxx11::basic_string<char> >::<lambda(auto:1)>' in a constant expression
   27 |      [[maybe_unused]] constexpr auto has_overload_to_string =
      |                                      ^~~~~~~~~~~~~~~~~~~~~~
prog.cc:28:21: note: 'foo<std::__cxx11::basic_string<char> >::<lambda(auto:1)>' is not literal because:
   28 |      hana::is_valid([](auto t) -> decltype(to_string(t)) {});
      |                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cc1plus: note:   'foo<std::__cxx11::basic_string<char> >::<lambda(auto:1)>' is a closure type, which is only literal in C++17 and later