typeof(typeOf、 TypeOfとも呼ばれる)は、変数のデータ型を決定するためにいくつかのプログラミング言語で提供される演算子です。これは、明示的に型を指定せずに複数の型のデータを受け入れる必要があるプログラムを構築するときに便利です
多態性と型キャストをサポートする言語では、typeof演算子はオブジェクトに適用された場合、2つの異なる意味を持つことがあります。Visual Basic [ 1]などの一部の言語では、typeof演算子はオブジェクトの動的な型を返します。つまり、型キャストに関係なく、オブジェクトの真の元の型を返します。これらの言語では、typeof演算子は実行時の型情報を取得するための方法です。
C# [2]やD [3] 、そしてC( C23以降)[4] [5]などの他の言語では、typeof 演算子はオペランドの静的型を返します。つまり、元の形式に関係なく、プログラムの実行時点で宣言された型に評価されます。これらの言語では通常、 typeidなど、実行時の型情報を取得するための他の構成要素が存在します。
例
C
C23 以降、typeofはC標準の一部です。演算子typeof_unqualも追加されました。これはと同じですがtypeof、cvr修飾とアトミック修飾を削除します(decltypeC++の演算子と区別します)。[6] [7]
C プログラミング言語の非標準 (GNU) 拡張では、typeof2 つのパラメータの最大値を決定するための一般的なマクロを定義するために使用できます。
#define max(a, b) ({ \
typeof (a) _a = (a); \
typeof (b) _b = (b); \
_a > _b ? _a : _b; \
})
C++
C++には演算子はありませんが、変数の型を表すために使用できる演算子がtypeofあります。C23で追加されましたが、C++には存在しません。C++には、型情報を取得するための演算子
もありますdecltypetypeoftypeid
構造体Double { double値; }
定数倍精度浮動小数点数型( Double )
// decltype は型宣言で使用できます
decltype ( d -> value ) y ; // double y と同等です。
// decltype は後続の戻り型で使用できます
// これは、たとえば、オーバーロードされた演算子で必要になる場合があります
// type T + type U は、T および U とは異なる型 V になる可能性があります
template < typename T , typename U > auto add ( T t , U u ) -> decltype ( t + u ) { return t + u ; }
C#
C# の場合:
// オブジェクトが与えられた場合、整数かどうかを返します。//
「is」演算子もこれを判断するために使用できます。public
static bool IsInteger ( object o ) { return o . GetType ( ) == typeof ( int ); }
Java
Javaには に相当するキーワードはありませんtypeof。すべてのオブジェクトはObjectのgetClass()メソッドを使用してクラスを返すことができ、そのクラスは常にそのクラスのインスタンスです。すべての型は、たとえクラスと見なされない場合でも、たとえば や のように、Class" " を末尾に追加することで明示的に名前を付けることができます。また、インスタンスとクラス名を受け取り、指定されたクラスのすべてのサブクラスに対して true を返す
型イントロスペクション演算子もあります.classint.classString[].classinstanceof
文字列s = "Hello, world!" ;クラス<?> sClass = s . getClass ();
Class < String > stringClass = String.class ; Class < Integer > intClass = int.class ;
Object obj = /* ここに何か */ ; System . out . printf ( "%s のクラスは %s%n です" , obj , obj . getClass (). getName ());
JavaScript
JavaScript では:
function isNumber ( n ) { return typeof n === 'number' ; }
TypeScript
TypeScriptの場合:[8]
function someFunction(param: typeof existingObject): void {
// ...
}
let newObject: typeof existingObject;
Python
Python has the built-in function type.[9]
print(type(123))
# prints: <class 'int'>
VB.NET
In VB.NET, the C# variant of "typeof" should be translated into the VB.NET's GetType method. The TypeOf keyword in VB.NET is used to compare an object reference variable to a data type.
The following example uses TypeOf...Is expressions to test the type compatibility of two object reference variables with various data types.
Dim refInteger As Object = 2
MsgBox("TypeOf Object[Integer] Is Integer? " & TypeOf refInteger Is Integer)
MsgBox("TypeOf Object[Integer] Is Double? " & TypeOf refInteger Is Double)
Dim refForm As Object = New System.Windows.Forms.Form
MsgBox("TypeOf Object[Form] Is Form? " & TypeOf refForm Is System.Windows.Forms.Form)
MsgBox("TypeOf Object[Form] Is Label? " & TypeOf refForm Is System.Windows.Forms.Label)
MsgBox("TypeOf Object[Form] Is Control? " & TypeOf refForm Is System.Windows.Forms.Control)
MsgBox("TypeOf Object[Form] Is IComponent? " & TypeOf refForm Is System.ComponentModel.IComponent)
See also
References
- ^ "TypeOf Operator (Visual Basic)". MSDN. Archived from the original on Nov 28, 2016.
- ^ "typeof (C#)". MSDN . 2016年9月10日時点のオリジナルよりアーカイブ。
- ^ “宣言 - Dプログラミング言語 1.0”. Digital Mars . 2012年12月30日. 2023年10月7日時点のオリジナルよりアーカイブ。
- ^ 「GNU コンパイラ コレクションの使用」の「Typeof」。
- ^ Meneide, JeanHeyd (2021年3月7日). 「Not-So-Magic - typeof(…) in C | r2」. Open Standards . 2021年12月2日閲覧。
- ^ “N2927: Not-so-magic - C言語のtypeof”. Open Standards . 2022年2月2日. 2023年12月1日時点のオリジナルよりアーカイブ。
- ^ 「remove_qualsの名前変更を検討してください」(PDF) . Open Standards . 2022年2月6日 . 2024年2月17日時点のオリジナルよりアーカイブ(PDF) .
- ^ 「'typeof' を使って型を推論する」Learn TypeScript . 2022年1月28日閲覧。
- ^ 「組み込み関数」。Pythonドキュメント。 2025年6月20日閲覧。