6 Replies - 816 Views - Last Post: 16 September 2016 - 01:50 PM

#1 zerophase  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 07-May 13

Instance and Class name matching compiles on Clang, but not MSVC

Posted 15 September 2016 - 01:34 AM

I'm just curious which part of the language MSVC leaves out, and if there is anyway to get instance and class name matching to compile in MSVC.

code:
typedef TFunction<FName(void)> Condition; // function pointer in unreal

class IGameTime
{
public:
        virtual ~IGameTime() {}
        virtual int Hour() = 0;
};

class GameTime : public IGameTime
{
public:
	virtual int Hour()
        {
             return 0;
        }
};

class SomeClass
{
public:
        void SomeMethod();
private:
        IGameTime *GameTime;
};

void SomeClass::SomeMethod()
{
    GameTime = new class GameTime();
	Condition c_0 = [this](void)->FName{ return (GameTime->Hour() < 19) ? TEXT("0x01000001000000D4") : TEXT("0x01000001000000E2"); };
}



The fact the constructor and variable name match causes MSVC to through compile errors on the lambda over syntax missing at the lambda. Changing the GameTime pointer to lower case fixes the issue on MSVC. (Should be lowercase anyways) This compiles fine with class in the initialization. So, is there anyway to get this to compile on MSVC without changing case, and which part of the C++ specification is left out in MSVC? (Couldn't find the precise line in the MSVC documentation)

This post has been edited by zerophase: 15 September 2016 - 02:36 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Instance and Class name matching compiles on Clang, but not MSVC

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5925
  • View blog
  • Posts: 20,255
  • Joined: 05-May 12

Re: Instance and Class name matching compiles on Clang, but not MSVC

Posted 15 September 2016 - 04:18 AM

Please post the exact error that you are getting. Do not paraphrase.

As an aside, you have a typo on line 6.

In general, though, considering how poor MSVC's track record of keeping up with current standards and compliance, I would not be too surprised if there is an issue.
Was This Post Helpful? 0
  • +
  • -

#3 zerophase  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 07-May 13

Re: Instance and Class name matching compiles on Clang, but not MSVC

Posted 15 September 2016 - 08:01 PM

Sorry, about that the precise errors are:
C2143 syntax error: missing ')' before '->'
C2059 syntax error: missing '->'
c2059 syntax error: missing ')'



That happens on every lambda assigned to a function pointer after:
Buffs = new class Buffs();


I get that the issue is the line before the lambda. Just found it kind of odd that MSVC doesn't support explicitly support declaring usage of a constructor.
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon

Reputation: 5344
  • View blog
  • Posts: 16,679
  • Joined: 25-December 09

Re: Instance and Class name matching compiles on Clang, but not MSVC

Posted 15 September 2016 - 08:06 PM

Please post the complete compiler error messages, exactly as they appear in your development environment. These messages have important information embedded within them to aid in locating and fixing the problems.

Also posting the smallest possible complete program that illustrates your problem is recommended.


Jim
Was This Post Helpful? 0
  • +
  • -

#5 zerophase  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 07-May 13

Re: Instance and Class name matching compiles on Clang, but not MSVC

Posted 16 September 2016 - 12:35 AM

I'm no longer getting the compiler errors, since I fixed the issues by Changing the GameTime instance to gameTime. Didn't need to specify class after new anymore to compile on Clang, and it would compile on MSVC as well that way. I was just curious if there's anyway in MSVC to have the default constructor and instance name match case wise.

This post has been edited by Skydiver: 16 September 2016 - 05:36 AM
Reason for edit:: Removed unnecessary quote. There is no need to quote the post directly above yours.

Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5925
  • View blog
  • Posts: 20,255
  • Joined: 05-May 12

Re: Instance and Class name matching compiles on Clang, but not MSVC

Posted 16 September 2016 - 05:59 AM

Frankly, I was surprised that this even compiles with the keyword class present after new:
class Foo
{
public:
    Foo() { }
};

Foo foo * = new class Foo();



I guess was just never exposed to that syntax before. I'd always seen that last line written as:
Foo foo * = new Foo;



Although, with C+11, I've been seeing this more:
Foo foo * = new Foo();



Now, regarding the lambda itself, why do you have that written this way:
Condition c_0 = [this](void) -> FName
                {
                    return (GameTime->Hour() < 19) ? TEXT("0x01000001000000D4")
                                                   : TEXT("0x01000001000000E2"); 
                };



It looks to me that there are no parameters and that the return type can be inferred by the compiler, so you'll end up with:
Condition c_0 = [this]
                {
                    return (GameTime->Hour() < 19) ? TEXT("0x01000001000000D4")
                                                   : TEXT("0x01000001000000E2"); 
                };



What is a bit surprising to me is how can your TEXT() macro actually return an FName function pointer. The standard Windows TEXT() macro returns a literal string. What magic happens to convert a string into a function pointer?
Was This Post Helpful? 0
  • +
  • -

#7 zerophase  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 07-May 13

Re: Instance and Class name matching compiles on Clang, but not MSVC

Posted 16 September 2016 - 01:50 PM

View PostSkydiver, on 16 September 2016 - 05:59 AM, said:

Frankly, I was surprised that this even compiles with the keyword class present after new:
class Foo
{
public:
    Foo() { }
};

Foo foo * = new class Foo();



I guess was just never exposed to that syntax before. I'd always seen that last line written as:
Foo foo * = new Foo;



The only reason I know about Foo *foo = new class Foo(); is Clang suggests it when it gets confused during initialization. I really like Clang for the increased compile speed, and improved compile errors. It'll actually find missing ; during class definitions. Switching from GCC to Clang took my parsers compile time down from 10 seconds to less than 1 second, as well.

View PostSkydiver, on 16 September 2016 - 05:59 AM, said:

Now, regarding the lambda itself, why do you have that written this way:
Condition c_0 = [this](void) -> FName
                {
                    return (GameTime->Hour() < 19) ? TEXT("0x01000001000000D4")
                                                   : TEXT("0x01000001000000E2"); 
                };



I was just being explicit with the lambda definition. Once I have all of the bugs in Unreal ironed out I might make it implicit. Unreal has multiple string types: FName, FString, FText I have a feeling that lambda might return an FString without being explicit. (FText is for strings needing locale support. FString allows for string manipulation, and I definitely don't want that for an id.

I could always define the lambda as:
Condition c_0 = [this]
                {
                      return (gameTime->Hour() < 19) ? FName(TEXT("0x01000001000000D4"))
                                                     : FName(TEXT("0x01000001000000E2"));
                }



It just feels a bit cleaner to me having the return type declared.

View PostSkydiver, on 16 September 2016 - 05:59 AM, said:

What is a bit surprising to me is how can your TEXT() macro actually return an FName function pointer. The standard Windows TEXT() macro returns a literal string. What magic happens to convert a string into a function pointer?


Since Unreal has their own implementation that replaces the Standard Library. Plus for historical reasons games don't touch STL. I'm sure, there's some of the Windows API buried deep in the Unreal code base, I just assume use the Unreal specific parts when programming for the engine. The TEXT() macro is Unreal specific, and looking at the code it looks like it formats strings so their different string classes can add identifiers to the string.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1