2017-09-09 3 views
1

なぜ行のconstexprの部分ですか?私はそれがインラインのようなものだと思った。 実際のタイプfgにありますか?なぜ型のconstexprの一部ですか?

この作品:

void f() 
{ 
    auto f = []() { return 42; }; 
    auto p = f; 

    static_assert(std::is_same_v<decltype(f), decltype(p)>); 
} 

が、これはしません:

void g() 
{ 
    constexpr auto f = []() { return 42; }; 
    auto p = f; 

    static_assert(std::is_same_v<decltype(f), decltype(p)>); 
} 

https://godbolt.org/g/Di4SJ5

+0

"works"とは、アサーションが合格するか失敗するのですか? –

答えて

5

constexprがオブジェクトconstを作るので、pがある一方でfのタイプは、constthe_lambda_typeです非const(これはewオブジェクトはfからコピーされました)。

オブジェクト宣言で使用されるconstexpr指定子は、constを意味します。

次のstatic_assertは問題ありません。

static_assert(std::is_same_v<std::remove_const_t<decltype(f)>, decltype(p)>); 

ところで:

gfの実際の型は何ですか?あなたは、例えば

constexpr auto f = []() { return 42; }; 
auto p = f; 

TD<decltype(f)> td1; 
TD<decltype(p)> td2; 

そのような

template <typename> 
struct TD; 

として、コンパイルエラーメッセージから実数型を取得するには、未定義のクラステンプレートを使用することができます

prog.cc:14:21: error: implicit instantiation of undefined template 'TD<const (lambda at prog.cc:9:24)>' 
    TD<decltype(f)> td1; 
        ^
prog.cc:15:21: error: implicit instantiation of undefined template 'TD<(lambda at prog.cc:9:24)>' 
    TD<decltype(p)> td2; 

、あなたは違いが表示されます:clangのエラーメッセージは次のようになります。

+0

'f()'は定数式ですか? (どちらの場合でも)、C++で17? –

+0

@ M.Mわからない。私は[ここ](https://wandbox.org/permlink/qcl0C80Tllyhx4qJ)を試しましたが、その答えは「constexpr」であろうとなかろうと、「はい」と思われます。 – songyuanyao

+0

定数式の結果はconstexprでなければなりません。そうでなければX と言うことはできません。 http://coliru.stacked-crooked.com/a/57ab736ff738f72b –

関連する問題