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

  • SEARCH
  • Home
  • 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 8651889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:10:41+00:00 2026-06-12T14:10:41+00:00

Clang-3.2 can compile and code behave as expected: struct have_f { int f(int i)

  • 0

Clang-3.2 can compile and code behave as expected:

struct have_f { int f(int i) {return 10;} }; 

struct empty {};

template <class T> 
struct outer {
        T t; 

        // if T have f(), define  outer_f()
        template<class U=decltype(t.f(1))> 
        int outer_f(int i) { return t.f(i); }
};

int main() {
        outer<have_f>  o1;
        outer<empty>   o2;

        // to silence unused var warning
        return  o1.outer_f(10) + sizeof(o2); 
}

GCC of any version rejects with:

t.cc:13:6: error: ‘struct empty’ has no member named ‘f’
  int outer_f(int i) { return t.f(i); }
      ^

Who is correct? Gcc or Clang?

Note, that there was similar question, without real answer.

  • 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-12T14:10:42+00:00Added an answer on June 12, 2026 at 2:10 pm

    I believe the issue is 14.6.3 [temp.nondep]:

    1 – Non-dependent names used in a template definition are found using the usual name lookup and bound at the point they are used.

    The example given describes that an ill-formed expression within the template definition “could be diagnosed either [in the template definition] or at the point of instantiation“.

    The default template-argument (14.1p9) U=decltype(t.f(1)) is a non-dependent name within the context of the instantiation of struct outer (that is, it does not depend on a template argument to its own template) so it is ill-formed for the instantiation of struct outer with T = struct empty. The standard doesn’t explicitly describe where default template-arguments are evaluated, but the only sensible conclusion is that they are treated as any other construct and evaluated at the point they occur (or, in this example, at the point of instantiation of the struct outer template). I don’t see any latitude for the compiler to delay evaluation of non-dependent default template-arguments to contexts where SFINAE applies.

    Fortunately the solution is easy: just make the default template-argument U a dependent name:

        // if T have f(), define  outer_f()
        template<class T2 = T, class U=decltype(static_cast<T2 &>(t).f(1))> 
        int outer_f(int i) { return t.f(i); }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using LLVM-clang on Linux. Suppose in foo.cpp I have: struct Foo { int
I have this code: template<typename T> class Listoid{ private: std::vector<T> list; public: typedef typename
Given an AST object in clang, how can I get the code behind it?
I'm trying to use SFHFKeychainUtils , but I can't compile without errors. I have
I want clang to compile my C/C++ code to LLVM bitcode rather than a
I try to compile the following code with clang (version 3.0), but it gives
I came across some code like this: struct A { A() {} A(int) {}
I have the following piece of code: typedef int AliasB; typedef unsigned short AliasA;
I try to compile NEON assembly code with LLVM clang integrated macro assembler (the
LLVM 2.6 + clang. Trying to compile C++ file and got: clang: warning: not

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.