Skip to content

C++20 の新機能 : constexpr関係追加 #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 23, 2019
Merged

C++20 の新機能 : constexpr関係追加 #20

merged 6 commits into from
May 23, 2019

Conversation

onihusube
Copy link
Contributor

constexpr関係のC++20新機能を書いてみました。

文章やコード内容はよしなになさってください!

@Reputeless
Copy link
Member

Reputeless commented May 22, 2019

ありがとうございます。確認中です。

c331258#diff-d21ab1223d831e3533f251ff5bbb105aR322

{
  constexpr int* nullp = nullptr;
  constexpr auto&& t = typeid(nullp);  //例外を投げるため定数実行できない
}

では例外を投げないので、

#include <typeinfo>

struct Cpp
{
	virtual ~Cpp() = default;
};

int main()
{
	constexpr Cpp* pCpp = nullptr;
	constexpr auto& cpptype = typeid(*pCpp); // error: 例外 std::bad_typeid を投げるため constexpr 不可
}

に修正します。
ほか、サンプルコードをコンパイル可能で短いものに置き換える予定です。

struct Cpp
{
	virtual int version() const = 0;
};

struct Cpp17 : Cpp
{
	constexpr int version() const override
	{
		return 17;
	}
};

struct Cpp20 : Cpp
{
	constexpr int version() const override
	{
		return 20;
	}
};

constexpr int GetVersion(const Cpp& a)
{
	return a.version();
}

int main()
{
	constexpr Cpp17 cpp17;
	constexpr Cpp20 cpp20;

	static_assert(GetVersion(cpp17) == 17);
	static_assert(GetVersion(cpp20) == 20);
}

また、c331258#diff-d21ab1223d831e3533f251ff5bbb105aR253
この変更に伴って std::type_info の operator== と operator!= がconstexpr指定され定数式で使用可能になります。 (P1328R0) は次回 2019-07 の標準会議で承認されるようなので、後日追記します。

@onihusube
Copy link
Contributor Author

すいません、typeidに関して勘違いを見つけたので少し直します

@onihusube
Copy link
Contributor Author

直しました

文章のスタイル調整
文章のスタイルを調整
@Reputeless Reputeless merged commit e2925fe into cppmap:master May 23, 2019
@Reputeless
Copy link
Member

Merged. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants