簡體   English   中英

gcc 6.1 std :: result_of編譯錯誤

[英]gcc 6.1 std::result_of compilation error

考慮一個小的獨立用例,其中我想確定一個類型是完整的還是不完整的使用

#include <type_traits>
namespace {

struct foo {
    template<class T, std::size_t = sizeof(T)>
    std::false_type  operator()(T&);
    std::true_type operator()(...);
};

struct FooIncomplete;
}


int main() {
    std::result_of<foo(FooIncomplete&)>::type();

    return 0;
}

這與gcc 4.9.3--std=c++11標志編譯良好。 但是,使用gcc 6.1--std=c++11會產生編譯錯誤

main.cpp: In function 'int main()':
main.cpp:17:5: error: 'type' is not a member of 'std::result_of<{anonymous}::foo({anonymous}::FooIncomplete&)>'
     std::result_of<foo(FooIncomplete&)>::type();

我在這里錯過了什么? 什么可能是一個可能的工作?

由於T不可調用,因此C ++ 14 result_of :: type不存在。

在你的情況下,struct FooIncomplete無需調用。

is_detected使用類似C ++ 20的is_detected

namespace details {
  template<template<class...>class Z, class, class...Ts>
struct can_apply:std::false_type{};
  template<class...>struct voider{using type=void;};
  template<class...Ts>using void_t = typename voider<Ts...>::type;

  template<template<class...>class Z, class...Ts>
  struct can_apply<Z, void_t<Z<Ts...>>, Ts...>:std::true_type{};
}
template<template<class...>class Z, class...Ts>
using can_apply=typename details::can_apply<Z,void,Ts...>::type;

template<class T>
using size_of = std::integral_constant<std::size_t, sizeof(T)>;

template<class T>
using is_complete = can_apply< size_of, T >;

我們得到一個特征is_complete ,如果我們可以將sizeof應用於T ,則為true。

請注意這一點,因為與大多數功能不同,類型的完整性可以在編譯單元之間改變,甚至可以在同一單元中的不同位置改變。 當類型some_template<some_args...>在程序中的不同位置發生變化時,C ++不喜歡它。

實例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM