f



SFINAE vs error reporting vs std::get

Hi!

I was just working on a generic `get_index_sequence<T>` metafunction 
that would automatically compute an index sequence for any type depending
on  the availability of `get<0>`, `get<1>`... for `T`.  My goal is to be
able to make a more generic version of the proposed `std::apply` that would
work for pairs, tuples, arrays and any user type that defines `get` for 0..N

However, this approach is not working on Clang 3.6 because Clang is "smart"
and uses a `static_assert` when `N` is out of range in `std::get<N>`.

I have experienced similar problems in the past with `common_type`,
`declval`,
etc. where static asserts would get in the way of certain SFINAE based
constructs.

The underlying problem is that there is a tension between being
SFINAE-friendly,
which requires silently disabling specializations or nested types, and being
user-friendly, which promotes loudly static asserting.

This makes me wonder:

- To what extent does the standard specify in which manner should be handled
  by library implementations?  (I remember, for example, common_type changed
  the wording in C++14 to be explictly SFINAE-friendly).

- In particular, should `std::get<N>` be SFINAE-friendly according to the
  standard wording?

- Is there a way to reconcile `static_assert` and SFINAE? I am thinking of,
  for example, adding a `static_catch(type expr)` thingy that if evaluating
  or instantiating the type in it results in a `static_assert`, the whole
  thing gracefully fails in a SFINAE-friendly way.  Or what if the rules of
  `static_assert` would be generally softened to be SFINAE-friendly, would
  that be too "unsafe"?

Thanks!

JP

 


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
raskolnikov
8/4/2015 9:49:32 AM
comp.lang.c++.moderated 10738 articles. 1 followers. allnor (8509) is leader. Post Follow

3 Replies
535 Views

Similar Articles

[PageSpeed] 55

On Tuesday, 4 August 2015 17:50:20 UTC+3, raskolnikov  wrote:
> I was just working on a generic `get_index_sequence<T>` metafunction 
> that would automatically compute an index sequence for any type depending
> on  the availability of `get<0>`, `get<1>`... for `T`.  My goal is to be
> able to make a more generic version of the proposed `std::apply` that
would
> work for pairs, tuples, arrays and any user type that defines `get` for
0..N
> 
> However, this approach is not working on Clang 3.6 because Clang is
"smart"
> and uses a `static_assert` when `N` is out of range in `std::get<N>`.

I do not understand what you mean by "smart". Can you be more specific
what you expect the library to do instead? There are overloads of 'std::get'
function required. For example that one:
 
    // in namespace 'std'
    template< size_t I, class T, size_t N > 
    constexpr T& get( array<T,N>& a );

It is required to check compile time that the 'I' is within bounds (unlike
'at'
that must do it runtime and 'operator[]' that is not required to check 
bounds). So some sort of compile-time diagnostic is required. That is the
whole point of using it instead of 'at' or '[]' that are required to have 
'constexpr' overloads as well. How you propose that check to be made?


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
ISO
8/5/2015 8:34:08 AM
On Wednesday, August 5, 2015 at 3:40:15 PM UTC+2, �� Tiib wrote:
>     // in namespace 'std'
>     template< size_t I, class T, size_t N > 
>     constexpr T& get( array<T,N>& a );
> 
> It is required to check compile time that the 'I' is within bounds (unlike
> 'at'
> that must do it runtime and 'operator[]' that is not required to check 
> bounds). So some sort of compile-time diagnostic is required. That is the
> whole point of using it instead of 'at' or '[]' that are required to have 
> 'constexpr' overloads as well. How you propose that check to be made?

There are at least two ways you can do such a check:

  - SFINAE friendly:

    template< size_t I, class T, size_t N > 
    constexpr auto get( array<T,N>& a )
      -> enable_if<(I >= 0 && I < N),
                    T&>;

  - Non sfinae friendly

    template< size_t I, class T, size_t N > 
    constexpr T& get( array<T,N>& a )
    {
      static_assert((I >= 0 && I < N), "out of bounds");
      ...
    }

My claim is that the former would allow to implement a generic function that
deduces the index sequence of a `get`-enabled data type, such as this:

    using std::get;
    struct could_not_get_index_sequence{};

    template <typename T, typename I, I N,
              typename Enable = void>
    struct get_integer_sequence_aux
    {
      using type = estd::conditional_t<
        (N == 0),
        could_not_get_index_sequence,
        estd::make_integer_sequence<I, N> >;
    };

    template <typename T, typename I, I N>
    struct get_integer_sequence_aux<
        T, I, N, estd::void_t<decltype(get<N>(std::declval<T>()))> >
      : get_integer_sequence_aux<T, I, N + 1>
    {};

    template <typename T, typename I>
    struct get_integer_sequence
      : get_integer_sequence_aux<T, I, 0>
    {};

Thanks a lot!

JP


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
raskolnikov
8/10/2015 6:43:56 AM
On Monday, 10 August 2015 13:50:16 UTC+2, raskolnikov  wrote:
> On Wednesday, August 5, 2015 at 3:40:15 PM UTC+2, 嘱 Tiib wrote:
> >     // in namespace 'std'
> >     template< size_t I, class T, size_t N > 
> >     constexpr T& get( array<T,N>& a );
> > 
> > It is required to check compile time that the 'I' is within bounds (unlike
> > 'at'
> > that must do it runtime and 'operator[]' that is not required to check 
> > bounds). So some sort of compile-time diagnostic is required. That is the
> > whole point of using it instead of 'at' or '[]' that are required to have 
> > 'constexpr' overloads as well. How you propose that check to be made?
> 
> There are at least two ways you can do such a check:
> 
>   - SFINAE friendly:
> 
>       [...]
> 
>   - Non sfinae friendly
> 
>       [...]
> 
> My claim is that the former would allow to implement a generic function that
> deduces the index sequence of a `get`-enabled data type, such as this:

Apologies if I'm being thick, but why is such a function useful? Is it not
much simpler to ask your "sequence" type to define a `size` metafunction 
�  la `std::tuple_size`, and then use `std::make_index_sequence<size<T>::value>`?

Also note that from a compile-time performance perspective, having to do all
this work (instantiating the N `get<>` functions in a SFINAE-able context) is
not great.

Regards,
Louis Dionne


-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Louis
8/11/2015 2:27:54 PM
Reply:

Similar Artilces:

Cellular automata benchmarks: Java vs C++ vs Java vs C++
I love benchmarking. Here are some very interesting results which compare different cellular automata implementations in C++ and Java, with different versions of the Java VM: Sun vs Microsoft (MS). MCell is a Windoze C++ program, MJCell is MCell ported to Java, SARCASim is a 5++ year old C++ Windoze program of mine and my Modern Cellular Automata Java applet is rather unique. Because of restrictions in the size of MJCell's universes almost all benchmarks were performed using an 800 by 600 universe. All benchmarks were performed using the Brian's Brain three state cellular a...

floating point programming: c/c++ vs matlab vs fortran vs ???
Dear all, I am helping someone code a model of an optical amplifier in C code (with the aim of verifying results published by someone else). The model seems to behave correctly for a limited range of parameters, but is quite off outside this range. This is unacceptable; AFAICT theres no algorithmic reason the model should fail, so I suspect things are going wrong in floating point land. As I've been reading up on the matter I find there is a lot that could be going wrong: floating point exceptions, rounding errors etc. The model can be run reasonably quick, so we'd happily sacrifice ...

Matlab vs C++ vs C#
Which of these should a person interested in financial modeling learn? "BB" <bb.donotmail@nomail.invalid> wrote in message news:ef1020b.-1@webx.raydaftYaTP... > Which of these should a person interested in financial modeling > learn? > If these are your only options, I would pick Matlab myself. You do much more with Matlab in much less time. Also debugging in Matlab is easy. Matlab also has a financial toolbox http://www.mathworks.com/access/helpdesk/help/toolbox/finance/finance.shtml Nasser Nasser Abbasi wrote: > > "BB" <bb.donotmai...

C vs. C++ vs. Java
Hi! I'dont have any experiences in programming... what language should I start learning???? C? C++ or Java`? greetz Start with C go on to C++ and then on to JAVA. And you will C what how that helps. priyasmita_guha@yahoo.co.in wrote: > Start with C go on to C++ and then on to JAVA. > And you will C what how that helps. The OP's question is not topical to this group. It would be better answered in comp.programming among others. Please try to redirect off topic questions rather than answering them here. hi there! i think u sho0uld start wid C language and LET US C is an x...

C++ vs Java vs OCaml vs SML
I have written a web page comparing the verbosity and speed of four different (progressively optimised) mini ray tracers written in C++, Java, OCaml and SML: http://www.ffconsultancy.com/free/ray_tracer/languages.html Essentially, the results indicate that Java is extremely slow (5x slower than the fastest implementation) and extremely verbose, OCaml is extremely concise (only 60 lines of code for the minimal implementation of the ray tracer) and Mlton-compiled SML is extremely fast (much faster than the other three languages). -- Dr Jon D Harrop, Flying Frog Consultancy http://www.ffco...

WHAT vs HOW vs WHERE
In the ongoing discussion here about alternatives to the RM, the subject of pointers keeps coming up. AFAIK, one of the features of the RM is that the interconnectedness of data is established by common data values, instead of pointers. In my view, pointers are "good things" not "bad things". But there are plenty of "good things" that will harm you if you don't use them right. One of the ways a network or graph data base achieves performance is by the efficient use of pointers, and the strategic preplacement of pointers where they will be need...

Android vs Qt vs C/C++
Hi All, I am a mobile application developer and this is the first time i have entered into developing an embedded application from scratch. I know that the title is a bit misleading and there will be rebuts in the grounds that Android is an OS and Qt is a framework and C/C++ is a language. Let me explain my requirement. I need to develop an embedded device which will be installed in a vehicle and it will periodically send telemetrics data to the cloud server using GSM modem. The telemetrics will include 1. GPS position data 2. Accelerometer readings 3. Image/Video data 4. It needs to li...

ex vs. em vs. px vs. % vs. in.
Hello All, I am looking for a for a good tutorial on font size definitions in a CSS. A URL to a good tutorial with demo/example will help. I am trying to create a CSS for DocBook XML Document, but I m unable to get a cross browser/platform compatibility. I want the documents to look fairly similar on all platforms/browsers. Here is my CSS -> http://www.xml-dev.com/blog/new.css And here are 2 plaint text documents using that CSS: http://tinyurl.com/yu3j8 http://tinyurl.com/2zwoh More documents here -> http://www.xml-dev.com/blog/#88 I was told that I should always use...

std::copy_backwards vs std::reverse_copy vs std::copy with reverse iterators
It seems all three can be used to accomplish almost identical tasks. Can someone chime in with their thoughts on their relative merits? As a concrete example, recently I was using reverse iterators to push_back on a vector in reverse order. std::vector<Elem> srcVec; std::vector<Elem> dstVec; std::copy(srcVec.rbegin(), srcVec.rend(), std::back_inserter(dstVec)); It occured to me that std::copy_backwards, and std::reverse_copy could be used to accomplish the same task, but I wasn't clear on why both alternatives are available. -- ...

Smalltalk vs. F-Script vs. Ruby vs. Objective-C
So many options, and they all claim they are heavily based on smalltalk. My plan is to learn smalltalk first (squeak or cincom visualworks?) but then I want to build a native Mac OS X app, that is, one that is not confined to living inside a smalltalk sandbox. One that uses the native aqua gui and uses core os x technology like coreimage, openGL and quartz. Which route would you recommend? Regards Marcel Marcel, > My plan is to learn smalltalk first Great plan! > (squeak or cincom visualworks?) VW is slicker, but check the licensing/cost befor...

JSP vs. Struts vs. JSF vs. Swing vs. Tapestry vs. Spring vs. Rails
I am a JSP developer and am constantly feeling like I am missing the boat by not using any of the other frameworks out there. However, the couple I have looked at (JSF and Rails) don't impress me. In JSP, I keep all of my logic in Servlets/Beans and all of my presentation in the JSP file, making custom tags when I need to do some HTML that involves some logic. So Servlets and Tags are the glue to the JSPs and Beans. I don't see what is wrong with this model. The biggest advantage is its flexible. An example of my frustation with JSF (which is different than all the other complaints...

Magic vs MiNT vs Gevena vs TOS vs...
Hi all, I'm starting this thread for 2 reasons: a) I'm a bit tired of reading the same subject again and again and decided to give it a little twist, b) Personal need. So, without any further ado, my situation is this: I have a stock STE with 4 Megs of memory, in which I'll be adding a hard drive (and maybe more) in this week. Being a floppy user (Atari-wise) all these years I never thought of having any other OSes than plain old TOS, but now, since I'll be getting some more space, I thought I might as well throw in a desktop/accs/resident progs setup there as well...

C vs. C++ and gcc/g++ vs. cc/CC
Having written a simple test application (which does the same in C and C++) I got the following results: with C++ the app compiled with g++ is about 20% faster than with the MIPSPro CC. with C the app compiled with MIPSPro cc is 3-4 times faster than compiled with gcc. The gcc/c app is 30-40% faster than the same g++/c++ app. The MIPS/c app is several times faster than the MIPS/c++ app. I always compiled with "-O3". For the C++ app I used valarrays, for the C app float[]. Is this the general performance line or is it only for my special test application? F...

error with std::string in header file using VS C++ 6.0 SP6
Can anybody help me with this code ? I contains errors I don't understand. I use Visual Studio C++ 6.0, Service Pack 6. CODE (header file) ==== #ifndef framing_h #define framing_h #include <vector> #include <string> std::string::size_type width (const std::vector<string>& v); std::vector<string> frame (const vector<string>& v); std::vector<string> vcat (const vector<string>& top, const vector<string>& bottom); std::vector<string> hcat (const vector<string>& left, const vector<string>& right); #end...

Web resources about - SFINAE vs error reporting vs std::get - comp.lang.c++.moderated

Reporting - Wikipedia, the free encyclopedia
Text is available under the Creative Commons Attribution-ShareAlike License ;additional terms may apply. By using this site, you agree to the ...

Spotlight review: Restrained, realistic view of investigative reporting provokes cold fury
Director Tom McCarthy's take on newspaper investigation into child sex abuse by Catholic Church a worthy Oscar contender.

Spotlight review: Restrained, realistic view of investigative reporting provokes cold fury
Director Tom McCarthy's take on newspaper investigation into child sex abuse by Catholic Church a worthy Oscar contender.

How Apple might avoid reporting a decline in iPhone sales
The crucial number in Apple's next quarterly earnings call, for Q1 in fiscal year 2016, is 74,468,000. That is the number of iPhone unit sales ...

Florida, Illinois latest in reporting Zika virus cases - Videos - CBS News
U.S. health officials are reporting new cases of a mosquito-borne virus linked to birth defects. Florida reported three confirmed cases of the ...

Ford changes pension reporting
Ford said it will restate its financial results since 2011 and show a $1.5 billion rise in 2015 pretax profit as it changes its pension reporting. ...

Apple investigating iPhone 6s models not correctly reporting battery life
If you have an iPhone 6s or iPhone 6s Plus that’s not properly showing how much battery you have left, there’s good news and bad news. Bad news ...

Germany’s Largest Broadcaster Apologizes For Not Reporting Sexual Assault Attacks
Germany’s Largest Broadcaster Apologizes For Not Reporting Sexual Assault Attacks

How the Kochs tried to destroy a journalist reporting on their political machine
... For her efforts she became a target of smear campaign. She writes about it in her book, as David Corn reports for Mother Jones. While reporting ...

Coaches, A.D. At Tennessee HS Charged For Not Reporting Sexual Assault In Hazing Incident
Criminal charges have been brought against the basketball coach, assistant coach, and athletic director at the Tennessee high school where three ...

Resources last updated: 1/25/2016 9:33:37 AM