Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8768781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:09:13+00:00 2026-06-13T17:09:13+00:00

I have following example code: @interface S1 : NSObject { void(*fn_)(); } @end @implementation

  • 0

I have following example code:

@interface S1 : NSObject
{
    void(*fn_)();
}
@end
@implementation S1
- (void) set:(BOOL)f
{
    if (f)
    {
        struct A { static void f() { std::cout << "1" << std::endl; } };
        fn_ = A::f;
    }
    else
    {
        struct A { static void f() { std::cout << "2" << std::endl; } };
        fn_ = A::f;
    }
}
- (void) test { fn_(); }
@end

struct S2
{
    void set(BOOL f)
    {
        if (f)
        {
            struct A { static void f() { std::cout << "1" << std::endl; } };
            fn_ = A::f;
        }
        else
        {
            struct A { static void f() { std::cout << "2" << std::endl; } };
            fn_ = A::f;
        }
    }
    void test() { fn_(); }
    void(*fn_)();
};

int main(int argc, const char * argv[])
{
    auto s1 = [[S1 alloc] init];
    [s1 set:TRUE];
    [s1 test];
    [s1 set:FALSE];
    [s1 test];

    S2 s2;
    s2.set(TRUE);
    s2.test();
    s2.set(FALSE);
    s2.test();

    return 0;
}

It prints

1
1
1
2

but I expecting

1
2
1
2

If I change name of second struct to different (e. g. “B”), always works as expected.

No warnings appears, so it’s hard to find why your program doesn’t work properly.

Is that my ignorance or llvm’s bug?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T17:09:14+00:00Added an answer on June 13, 2026 at 5:09 pm

    It would indeed appear to be a clang bug. (with the caveat that there is no standard specifying the Objective-C++ language, so what is “correct” is a little bit up in the air)

    If instead of compiling to executable code, I generate LLVM IR, using a command like this

    clang -g -std=c++11  -Wall -Wextra localstruct.mm  -emit-llvm -S
    

    the -set: method compiles down to this, with what would appear to be the two function pointer assignment lines highlighted:

    define internal void @"\01-[S1 set:]"(%0* %self, i8* %_cmd, i8 signext %f) uwtable ssp {
      %1 = alloca %0*, align 8
      %2 = alloca i8*, align 8
      %3 = alloca i8, align 1
      store %0* %self, %0** %1, align 8
      call void @llvm.dbg.declare(metadata !{%0** %1}, metadata !1490), !dbg !1491
      store i8* %_cmd, i8** %2, align 8
      call void @llvm.dbg.declare(metadata !{i8** %2}, metadata !1492), !dbg !1491
      store i8 %f, i8* %3, align 1
      call void @llvm.dbg.declare(metadata !{i8* %3}, metadata !1493), !dbg !1494
      %4 = load i8* %3, align 1, !dbg !1495
      %5 = icmp ne i8 %4, 0, !dbg !1495
      br i1 %5, label %6, label %12, !dbg !1495
    
    ; <label>:6                                       ; preds = %0
      %7 = load %0** %1, align 8, !dbg !1497
      %8 = load i64* @"OBJC_IVAR_$_S1.fn_", !dbg !1497, !invariant.load !1499
      %9 = bitcast %0* %7 to i8*, !dbg !1497
      %10 = getelementptr inbounds i8* %9, i64 %8, !dbg !1497
      %11 = bitcast i8* %10 to void ()**, !dbg !1497
    

    store void ()* @"_ZN10-[S1 set:]1A1fEv", void ()** %11, align 8, !dbg !1497

      br label %18, !dbg !1500
    
    ; <label>:12                                      ; preds = %0
      %13 = load %0** %1, align 8, !dbg !1501
      %14 = load i64* @"OBJC_IVAR_$_S1.fn_", !dbg !1501, !invariant.load !1499
      %15 = bitcast %0* %13 to i8*, !dbg !1501
      %16 = getelementptr inbounds i8* %15, i64 %14, !dbg !1501
      %17 = bitcast i8* %16 to void ()**, !dbg !1501
    

    store void ()* @"_ZN10-[S1 set:]1A1fEv", void ()** %17, align 8, !dbg !1501

      br label %18
    
    ; <label>:18                                      ; preds = %12, %6
      ret void, !dbg !1503
    }
    

    Both cases appear to reference the same function symbol _ZN10-[S1 set:]1A1fEv. If you look at the corresponding code for the method in the struct, it references two: _ZZN2S23setEaEN1A1fEv and _ZZN2S23setEaEN1A1fE_0v.

    FWIW, GCC’s Objective-C++ compiler produces the desired result. Please do report the bug to the clang project and don’t just work around the problem in your code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say suppose I have the following Java code. public class Example { public static
I have the following example code using MEF: public interface IFoo<T> {} public class
Code I have the following class with a member interface: package com.example.withinterface; public class
I have the following example code: $dataProvider = new CActiveDataProvider('firstTable', array('criteria' => array( 'select'
Say I have the following code: <div onclick='location.href=http://www.example.com/'> <a href='#' onclick='alert(blah)'>click</a> </div> Is there
On a report I have the following code for a field: =Sum([PartQty]*[ModuleQty]) Example results
I have the following base interface public interface IHandler{ void Handle(IMessage message); } and
I have following code: package main import ( fmt ) type Point struct {
I have the following native function interface in C++: int func1(void* param, int sizeOfParam).
I have the following piece of code, as an example dec_proxy attempts to reverse

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.