0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

C++20 でテンプレートで準拠モードでコンパイルエラー

0
Last updated at Posted at 2026-07-30

C++20で準拠モードが「はい」だと、以下のコードで C2065, C3861 が発生する。C++14では発生しない。

template<typename T>
struct Hoge
{
  T a ;
  T b ;
} ;

template<typename T>
struct DerivedHoge
  : Hoge<T>
{
  T GetA() const
  {
    return a ;
  }
  T GetB() const
  {
    return b ;
  }
} ;

using DerivedHogeD = DerivedHoge<double> ;

void func()
{
  DerivedHogeD a ;
  a.GetA() ;
  a.GetB() ;
}

image.png

テンプレートだと、DerivedHoge側で親の変数であるaとbを認識できないことが原因らしい。準拠モードを「いいえ」にすれば回避できるが、C++のバージョンが上がるたびに厳しくなっているようなので、いたちごっことなってしまう。
コード上の対処法としては、以下の2つがある。

  1. __super::をつける
  2. usingをつける
template<typename T>
struct DerivedHoge
  : Hoge<T>
{
  using Hoge<T>::b ; // ここ

  T GetA() const
  {
    return __super::a ; // ここ
  }
  T GetB() const
  {
    return b ;
  }
} ;

では。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?