dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Can you add custom keywords in VS2017?

  1. #1
    Join Date
    Nov 2018
    Posts
    19

    Can you add custom keywords in VS2017?

    For instance if I had to add the word "in" as a keyword so that I can check if an element is in a container using the in keyword, is that possible?

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,544

    Re: Can you add custom keywords in VS2017?

    The simple answer is no. There is a macro pre-processor available which in some circumstances can be used to provide user-defined extensions to the language. However, this macro pre-processing is performed prior to the compiler. The defined macros can take arguments - so you could define a macro called in(x, y) and use it to expand to the appropriate container syntax. However if you want to write something like if (x in y) then no. You would need to write if (in(x, y)). This could be done using a proper function/lambda which would be better. There are also numerous pitfalls when using macros and their use is discouraged.

    Why would you want to do this?

    See https://docs.microsoft.com/en-us/cpp...r?view=vs-2017 for info about the pre-preprocessor.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.9.4)

  3. #3
    Join Date
    Nov 2018
    Posts
    19

    Re: Can you add custom keywords in VS2017?

    Thanks 2kaud you're very knowledgeable!

  4. #4
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,544

    Re: Can you add custom keywords in VS2017?

    Note that you could define an operator overload to provide the required action. Eg, you could define operator & so you could write (if x & y) where x is a container and y is an element.

    See also
    https://stackoverflow.com/questions/...operators-in-c
    https://stackoverflow.com/questions/...or-overloading

    However, the basic common rule for operator overloading is the overload works 'as expected' for the given data types. However there is nothing in the language to stop you inventing your own rules for what an operator overload does. Note that you cannot overload an already defined operator!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.9.4)

  5. #5
    Join Date
    Feb 2017
    Posts
    389

    Re: Can you add custom keywords in VS2017?

    Quote Originally Posted by Numb View Post
    For instance if I had to add the word "in" as a keyword so that I can check if an element is in a container using the in keyword, is that possible?
    That would require the C++ standard itself to be programmable. C++ would become a language of laterally incomprehensible dialects. Although possible, I think the idea is too revolutionary for an existing language. Maybe a new experimental language could pick it up.

    But there already is a feature of C++ I think could develop in the direction of "adding own keywords" and that's metaprogramming. It doesn't change the syntax of C++ but it allows parts of programs to run at compile-time, setting the stage for what happens at runtime.

    Note that the notion of "adding own keywords" exists is some languages such as Forth and Lisp but that doesn't mean the syntax can be modified by the programmer. It's more like a mental picture of how to develop programs in those languages.
    Last edited by wolle; January 11th, 2019 at 12:00 AM.

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    12,898

    Re: Can you add custom keywords in VS2017?

    One little additional nitpick, the question should be asked around Visual C++ 2017 rather than VS 2017. As mentioned previously, Visual Studio is an IDE container that supports multiple languages like C#, VB.Net, C++ and so on. As such, VS itself provides no language specific extensibilty.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)