1

Is there a convenient way to check if a string value is a type?

It's possible to have a list of built-in types to compare against, but that wouldn't work for user defined types.

std::string s = "std::vector<int>";
tuk
  • 367
  • 1
  • 4
  • 10
  • 6
    No, and in fact it is impossible to exhaustively check at all. This screams of XY problem though. What do you need this for? – user17732522 Jan 29 '23 at 11:13
  • I need to parse and tokenize c++ code from a text file and type syntax is complicated in the context of pattern recognition. – tuk Jan 29 '23 at 11:16
  • @Tuk Your task is more or less comparable to writing a C++ compiler. If it is an option I would pick an easier language to parse. – john Jan 29 '23 at 11:22
  • 2
    *and parsing types is complicated* -- This [has already been done](https://stackoverflow.com/questions/17388771/get-human-readable-ast-from-c-code). Generate the AST from the C++ code, and parse that with an XML parser. – PaulMcKenzie Jan 29 '23 at 11:23
  • 1
    Try to compile ```R"(#include ... int main(){ @@yourString@@ var; std::cout<<"size="< – huseyin tugrul buyukisik Jan 29 '23 at 11:26
  • @tuk It is basically impossible to parse C++ without building a full C++ compiler. It isn't even possible to parse expressions without being able to do overload resolution and in turn compile-time evaluation. – user17732522 Jan 29 '23 at 11:27
  • Voted down for what? ..I guess we'll never know! Can it really be "impossible to exhaustively check at all." is that not what a complier is doing when it reads the source files? Unfortunately the code being parsed has to be c++ – tuk Jan 29 '23 at 11:27
  • 2
    You're essentially asking: how do I build a C++ frontend? That's not remotely answerable in a post. – Passer By Jan 29 '23 at 11:28
  • 1
    @tuk It is impossible to do from inside the C++ language. If you want to write a C++ compiler that externally parses a source code that is of course possible. – user17732522 Jan 29 '23 at 11:29
  • @huseyintugrulbuyukisik Yes, my compiler complains about missing headers. – Passer By Jan 29 '23 at 11:32
  • @PasserBy platform dependency is an issue ofcourse. – huseyin tugrul buyukisik Jan 29 '23 at 11:33
  • Maybe you can use libclang to do the parsing for you? https://shaharmike.com/cpp/libclang/ – Pepijn Kramer Jan 29 '23 at 11:37
  • 1
    @tuk -- The C++ compiler knows what everything is, otherwise it wouldn't be able to compile the code. Thus the way you "parse code" is to query the compiler's internal view of the source, and that is exactly what clang's toolset gives you -- a hierarchical breakdown of the variables, types, functions, etc. – PaulMcKenzie Jan 29 '23 at 11:44
  • https://stackoverflow.com/questions/41453/how-can-i-add-reflection-to-a-c-application – Hans Passant Jan 29 '23 at 11:50
  • *How to check if a string value is a type?* If you have a limited number of types to care about, make a `std::map` of string key to type. – Eljay Jan 29 '23 at 12:01

2 Answers2

4

Internally, it is impossible to verify whether a string would be a type, simply because it is impossible to reflect on what types are defined in the program.

And externally, it is impossible to tell whether a given token sequence is a type in C++ without essentially having a complete C++ compiler/interpreter at hand. For example:

template<bool>
struct A {
    using X = int;
};

template<>
struct A<false> {
    constexpr static auto X = []{};
};

constexpr bool some_function() { /*...*/ }

int main() {
    A<some_function()>::X();
}

Whether A<some_function()>::X here is a type or an expression depends on whether or not some_function() evaluates to true or false at compile-time and the compile-time language is essentially Turing-complete (if there wasn't an implementation-defined limit on number of operations), requiring evaluating almost unrestricted C++ itself.

Even worse, the same is required to even parse C++ expressions, because the meaning of any given < token can also depend on arbitrary compile-time evaluation.

user17732522
  • 53,019
  • 2
  • 56
  • 105
-3

If it's only your computer to run it, then prepare a header to include everything like this in GCC:

bits/stdc++.h

then construct a string like this (pseudo-code):

bool isThisType(std::string userType)
{
    std::string toCompile = R"(
         #include<bits/stdc++.h> 
         #include<listOfBuiltinTypes>
         int main(){ 
            @@yourString@@ var; 
            std::cout<<"size="<<sizeof(var); 
            return 0;  
         }
    )";
    replace(toCompile,"@@yourString@@",userType);
    auto fileName = commandLineWriteToFile(toCompile); // main.cpp
    auto errorString = commandLineCompileFile(fileName); // "error.."
    bool isType=!commandLineCheckErrorOutput(errorString);
    return isType;
}
...
bool tmp = isThisType("std::vector<int>");

otherwise you'd need to make a C++ compiler yourself.

huseyin tugrul buyukisik
  • 11,469
  • 4
  • 45
  • 97