SFINAE: Detecting if a function is called by a compile time known value
Asked Answered
R

1

6

I like to do some check when one of my ctors are called with compile time known value. Is there a way to detect it?

So when someone call this:

A a (10);

since 10 is a compile time known constant, i like to call a special ctor, like this:

template<int Value, typename = std::enable_if_t<Value <= 100>>
A (int Value) {}

Any idea how can i solve this issue? Thanks!

Reorganize answered 15/12, 2016 at 15:40 Comment(0)
S
4

Integral constant could solve your problem:

struct A {
    template<int v, std::enable_if_t<(v <= 100)>* = nullptr>
    A(std::integral_constant<int, v>) {}
};

Then, you can use it like this:

A a{std:integral_constant<int, 7>{}};

For ease of use, you could also use something similar to what boost::hana does. It define a literal operator that transform the number into an integral constant:

A a{76_c}; // the ""_c operator outputs an std::integral_constant<int, 76>

You can read more about this operator in the boost::hana documentation

Strawboard answered 15/12, 2016 at 16:16 Comment(11)
Thanks for the idea, unfortunately it's not. A really important point of my lib is that it have to be as easy to use as simple primitive types. Like 99% of C++ programmers doesn't even know about integral_constant.Reorganize
This is actually a very nice solution, +1, and the rest of 99% of C++ programmers should learn about integral_constant :) I'd definitely go this route if what you want it simply testing the values of some constants at compile time.Cusk
@Cusk That's the context: I am working on an invariant lib which meant to replace asserts (or other type of checks) based on primitive types and put them into type information. The aim is to be just as simple as primitive types, but produce nicer and more efficient code. This integral_constant version is a nice addition, but my aim is to optimize away things silently, without the users have to even think about it. Sure, some will use integral_constant, but most not, and honestly, if they have to use it, the code won't be nicer, which something i want to achieve.Reorganize
@GuillaumeRacicot: thanks, that's much better! Tho i still aiming for a solution which compile my original code. If noone can give me such an answer i'll probably accept yours.Reorganize
Make it inline and call it a day. The compiler can optimize better than most of us.Eider
The compiler cant optimise away in my case as i want to do completely different thing in the compile time version. And anyway, compilers do alot of awesome thing, and miss alot of obvious stuff, so i dont want to rely on them too much.Reorganize
@GuillaumeRacicot: The hana doc say "This user-defined literal is an extension which requires a special string literal operator that is not part of the standard yet." As i know it won't be shipped with C++17. :(Reorganize
@Melcon That's why we have the if statement in C++: to do a completely different thing when a condition is met.Eider
@Reorganize this message is about the hana::string literal operator. We are using the literal operator on a number in this solution.Strawboard
@Reorganize _c does not need that extension, but _s does.Strawboard
@n.m. completely unrelated.Strawboard

© 2022 - 2024 — McMap. All rights reserved.