Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #86793

Re: Is this valid C++ code?

From Bo Persson <bo@bo-persson.se>
Newsgroups comp.lang.c++
Subject Re: Is this valid C++ code?
Date 2022-10-04 13:47 +0200
Message-ID <jq2kr7Ft790U1@mid.individual.net> (permalink)
References <bc46c146-abb5-4b16-b62d-324e049c7083n@googlegroups.com>

Show all headers | View raw


On 2022-10-03 at 23:09, daniel...@gmail.com wrote:
> Hello everyone,
> 
> Is the C++ code below valid?
> 
> #include <iostream>
> #include <string_view>
> #include <string>
> 
> template <class T, class Enable = void>
> class Foo
> {};
> 
> template <class T>
> class Foo<T,typename std::enable_if<std::is_same<T,std::string>::value>::type>
> {
> public:
>      
>      void f(std::string s)
>      {
>          Foo<std::string_view> foo; // (*)
>          foo.f(s);
>      }
> };
> 
> template <class T>
> class Foo<T, typename std::enable_if<std::is_same<T, std::string_view>::value>::type>
> {
> public:
>      
>      void f(std::string_view s)
>      {
>          std::cout << s << "\n";
>      }
> };
> 
> int main()
> {
>      Foo<std::string> foo;
>      foo.f("Hello World");
> }
> 
> LLVM (clang-cl) says "yes", and outputs "Hello World".
> 
> Visual Studio 2022 (v143) says "no", "'f': is not a member of 'Foo<std::basic_string_view<char,std::char_traits<char>>,void>",		17	
> at line (*)
> 
> Both compiled with C++17.
> 

Seems like g++ (v12.2) agrees with VC++, and also points out the problem 
that the specialization is defined after its first use.

My (non-language-lawyer) interpretation is that since f and its local 
foo don't depend on the template parameter T, they should be looked up 
immediately, and not at the point of instantiation.

Back to comp.lang.c++ | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Is this valid C++ code? "daniel...@gmail.com" <danielaparker@gmail.com> - 2022-10-03 14:09 -0700
  Re: Is this valid C++ code? David LaRue <huey.dll@tampabay.rr.com> - 2022-10-03 23:52 +0000
    Re: Is this valid C++ code? "daniel...@gmail.com" <danielaparker@gmail.com> - 2022-10-03 18:07 -0700
      Re: Is this valid C++ code? "daniel...@gmail.com" <danielaparker@gmail.com> - 2022-10-03 18:42 -0700
    Re: Is this valid C++ code? Juha Nieminen <nospam@thanks.invalid> - 2022-10-04 06:05 +0000
  Re: Is this valid C++ code? Bo Persson <bo@bo-persson.se> - 2022-10-04 13:47 +0200
    Re: Is this valid C++ code? "daniel...@gmail.com" <danielaparker@gmail.com> - 2022-10-04 05:22 -0700
    Re: Is this valid C++ code? Manfred <noname@add.invalid> - 2022-10-05 02:54 +0200
  Re: Is this valid C++ code? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-10-07 12:41 -0700
    Re: Is this valid C++ code? Andrey Tarasevich <andreytarasevich@hotmail.com> - 2022-10-08 11:18 -0700

csiph-web