Code Monkey home page Code Monkey logo

Comments (10)

am11 avatar am11 commented on June 9, 2024

@Snaipe, can you please make a more self-contained example (with main instantiating a &ofstream and then expected/actual outputs)? I will pass it along to some concerned folks. :)

from criterion.

Snaipe avatar Snaipe commented on June 9, 2024

@am11 I managed to come up with a self-contained example after a bit of tweaking around.

The issue arises only when:

  1. the stream class is inherited.
  2. the variable is typed as a reference of the inherited type (using a reference to std::fstream, for instance, works).

It seems to be a problem with the overload resolution picking up operator<< for const void * before the one for const char *.

GCC & Clang seem to both handle the resolution gracefuly.

from criterion.

am11 avatar am11 commented on June 9, 2024

@Snaipe, thanks! On compiling with g++ 4.9, I am getting:

sh-4.3$ g++ --std=c++0x -o main *.cpp                                                                                                                                              

main.cpp: In instantiation of 'my_basic_fstream<CharT>::my_basic_fstream(FILE*) [with CharT = char; FILE = _IO_FILE]':                                                             

main.cpp:21:13:   required from 'static my_basic_fstream<CharT>& get_file_stream_<CharT>::call(FILE*) [with CharT = char; FILE = _IO_FILE]'                                        

main.cpp:28:36:   required from here                                                                                                                                               

main.cpp:11:64: error: no matching function for call to 'std::basic_fstream<char>::basic_fstream(FILE*&)'                                                                          

     my_basic_fstream(std::FILE *f) : std::basic_fstream<char>(f) {}                                                                                                               

                                                                ^                                                                                                                  

main.cpp:11:64: note: candidates are:                                                                                                                                              

In file included from main.cpp:3:0:                                                                                                                                                

/usr/include/c++/4.9.2/fstream:834:7: note: std::basic_fstream<_CharT, _Traits>::basic_fstream(const string&, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_tra

its<char>; std::string = std::basic_string<char>; std::ios_base::openmode = std::_Ios_Openmode]                                                                                    

       basic_fstream(const std::string& __s,                                                                                                                                       

       ^                                                                                                                                                                           

/usr/include/c++/4.9.2/fstream:834:7: note:   no known conversion for argument 1 from 'FILE* {aka _IO_FILE*}' to 'const string& {aka const std::basic_string<char>&}'              

/usr/include/c++/4.9.2/fstream:819:7: note: std::basic_fstream<_CharT, _Traits>::basic_fstream(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_trait

s<char>; std::ios_base::openmode = std::_Ios_Openmode]                                                                                                                             

       basic_fstream(const char* __s,                                                                                                                                              

       ^                                                                                                                                                                           

/usr/include/c++/4.9.2/fstream:819:7: note:   no known conversion for argument 1 from 'FILE* {aka _IO_FILE*}' to 'const char*'                                                     

/usr/include/c++/4.9.2/fstream:806:7: note: std::basic_fstream<_CharT, _Traits>::basic_fstream() [with _CharT = char; _Traits = std::char_traits<char>]                            

       basic_fstream()                                                                                                                                                             

       ^                                                                                                                                                                           

/usr/include/c++/4.9.2/fstream:806:7: note:   candidate expects 0 arguments, 1 provided                                                                                            

from criterion.

am11 avatar am11 commented on June 9, 2024

I have further simplified the example with https://gist.github.com/am11/5341b87ce9eb6bdf2d15; added some comments. Removing one (apparently) unrelated and unused code line makes it compile and pass with VCR. But Clang and GCC failed due to same issue (that they give with the original example): missing function signature std::basic_fstream which takes std::File*.

from criterion.

Snaipe avatar Snaipe commented on June 9, 2024

@am11 the sample is using a MSVC-specific constructor (the one taking a FILE*), I did not make the GCC/Clang specific version because it did not happen on both of them.

I added a version which is GCC/Clang compatible, but it's not that interesting to be honest.

from criterion.

am11 avatar am11 commented on June 9, 2024

@Snaipe, I agree its looks like spaghetti now. 😜
Nonetheless, I will proceed with reporting to VC team and cross-reference the issue here. FWIW, we reported some bugs after VS2015 RTM (released 29.07.2015) and they have fixed for VS2015 Update 1 (currently unreleased). For some reason their webcompiler site is down: http://webcompiler.cloudapp.net/, otherwise we could verify this one right away.
Thank you for this effort! 👍

from criterion.

am11 avatar am11 commented on June 9, 2024

x-ref: https://connect.microsoft.com/VisualStudio/feedback/details/1816111.

from criterion.

Snaipe avatar Snaipe commented on June 9, 2024

@am11 I just reread the snippet you linked, but I think that the solution you provide to make it pass is a bit unfortunate: *unused_stream actively invokes undefined behaviour (you dereference a null smart pointer), and, well, the apparent "good" behaviour could be the result of an optimisation based on the fact that *unused_stream is invalid.

from criterion.

am11 avatar am11 commented on June 9, 2024

@Snaipe, I have received a reply (via private email) from VC team member (Stephan T. Lavavej aka STL):

I've reduced this and I believe it is a compiler bug. Here's a library-free repro:

C:\Temp>type meow.cpp

namespace Std {

    template <class Elem> struct CharTraits { };

    template <class Elem, class Traits = CharTraits<Elem>> struct BasicOstream {
        void operator<<(const void *) = delete;
    };

    template <class Traits> void operator<<(BasicOstream<char, Traits>&, const char *) = delete;
}

template <typename CharT> struct MyOstream : Std::BasicOstream<char> { };

MyOstream<char> * stream;

#ifdef AVOID_TEMPLATE
    MyOstream<char>& call() {
#else
    template <typename RandomT = double> MyOstream<char>& call() {
#endif
    stream = new MyOstream<char>();
    return *stream;
}

int main() {
    MyOstream<char>& f = call();
    f << "Foo";
}

C:\Temp>cl /EHsc /nologo /W4 meow.cpp

meow.cpp

meow.cpp(26): error C2280: 'void Std::BasicOstream<char,Std::CharTraits>::operator <<(const void *)': attempting t

o reference a deleted function

    with

    [

        Elem=char

    ]

meow.cpp(5): note: see declaration of 'Std::BasicOstream<char,Std::CharTraits>::operator <<'

    with

    [

        Elem=char

    ]

C:\Temp>cl /EHsc /nologo /W4 /DAVOID_TEMPLATE meow.cpp

meow.cpp

meow.cpp(26): error C2280: 'void Std::operator <<Std::CharTraits(Std::BasicOstream<Elem,Std::CharTraits> &

,const char *)': attempting to reference a deleted function

    with

    [

        Elem=char

meow.cpp(8): note: see declaration of 'Std::operator <<'

AVOID_TEMPLATE shouldn't affect the result of overload resolution, but it does. I'll pass this along to the compiler team.

from criterion.

Snaipe avatar Snaipe commented on June 9, 2024

Awesome.

In the meantime, I guess I can remove the template on call (we use CharT = char anyways) since it's affecting resolution.

from criterion.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.