LoginSignup
5
2

More than 3 years have passed since last update.

decltypeとtemplate関数ポインタと関数オーバーロードを使っていたらMSVCがバグってた

Posted at

はじめに

小ネタです。

サンプルコード

#include <iostream>
#include <typeinfo>

template<class T>
T func(T a)
{
    return a;
}

int func(int a)
{
    return a;
}

int main()
{
    std::cout << typeid(decltype(func<int>)).name() << std::endl;
}

このコードはコンパイルができますか?

はい。gccとclangではコンパイルができました。
https://wandbox.org/permlink/gnpV6M5k6HGB7aZa

しかし

msvcではコンパイルができませんでした。
なんでや…

    decltype(func<int>) a;
    std::cout << typeid(a).name() << std::endl;

ちなみに上記のようにしたらコンパイルが通った

サンプルコード2

#include <iostream>
#include <typeinfo>

template<class T>
T func(T a)
{
    return a;
}

int func(int a)
{
    return a;
}

int main()
{
    decltype(&func) a;
    std::cout << typeid(a).name() << std::endl;
}

このコードはコンパイルができますか?

はい。gccとclangではコンパイルが失敗しました。
https://wandbox.org/permlink/SFzOCsAb2V5GHPL1

gccのエラー

'decltype' cannot resolve address of overloaded function

clangのエラー

reference to overloaded function could not be resolved; did you mean to call it?

オーバーロードされてるから解決できないとのことです。

しかし

msvcではコンパイルができました。(波線はでるんですけどね)
なんでや…

aはint(*)(int)型になってますね

まとめ

MSVCは今日もバグっていました。(たぶん)
みんなもMSVCのバグには気を付けよう。

自分の書いたコードが他のコンパイラでも通るかしっかり確かめよう。

5
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
2