質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

Q&A

解決済

1回答

1555閲覧

Must template argument functions be treated as potentially constexpr?

onitail

総合スコア12

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

0グッド

0クリップ

投稿2015/02/17 04:01

Consider this program:

template <int F(), int N = F()>
void f() { }
constexpr int g() { return 1; }
int main() { f<g>(); }
Is this valid? Are compilers required to see at template definition time, that F could potentially refer to a constexpr function, and therefore the default argument for N could be valid?

gcc and clang accept this, but Intel rejects the template function at template definition time because F() is not a constant expression. Intel does accept f<g, g()>() if the default argument is removed, so clearly it understands that g() is usable in a constant expression in general.

It's not clear to me what the standard says. It's clear that (C++11 [expr.const]p2)

an invocation of a function other than a constexpr constructor for a literal class or a constexpr function
renders an expression non-constant, but it's not clear to me whether that applies here. At template definition time, it certainly does seem to apply, since F is not declared to be a constexpr function, but at the same time, errors at template definition time are supposed to be diagnosed only if there is no possible valid instantiation of the template, and there does appear to be a valid instantiation here.

I can see the arguments for both answers, so I'm getting confused. Is there a definitive answer to this question?

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

The following Q&A and its comments on StackOverflow might help you. I think that last one answer to your question.

投稿2015/02/17 06:34

編集2015/02/17 07:28
yohhoy

総合スコア6189

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問