This question already has answers here:
static const Member Value vs. Member enum : Which Method is Better & Why?
(6 answers)
Closed 10 years ago.
I stumbled upon this answer to a question about powers of integers in c++: https://stackoverflow.com/a/1506856/5363
I like it a lot, but I don't quite understand why the author uses one element enums as opposed to some integer type explicitly. Could someone explain?
AFAIK this has to do with older compilers not allowing you to define compile-time constant member data. With C++11 you could do
template<int X, int P>
struct Pow
{
static constexpr int result = X*Pow<X,P-1>::result;
};
template<int X>
struct Pow<X,0>
{
static constexpr int result = 1;
};
template<int X>
struct Pow<X,1>
{
static constexpr int result = X;
};
int main()
{
std::cout << "pow(3,7) is " << Pow<3,7>::result << std::endl;
return 0;
}
Related
This question already has answers here:
Why do the usual access control checking applies to names used to specify explicit instantiation when accessed through template parameters?
(1 answer)
Why does a hole for access checking exist for explicit template instantiations? [duplicate]
(1 answer)
Closed 4 months ago.
Declaring a member private means there should never be a legal way to access it directly from outside. But template explicit instantiation breaks the law. What's the consideration of this? Or it's a defect?
Example:
#include <cassert>
#include <iostream>
class A {
public:
int X() { return x_; }
private:
int x_;
};
int A::*FiledPtr();
template <int A::*M>
struct Rob {
friend int A::*FiledPtr() { return M; }
};
template struct Rob<&A::x_>;
int main() {
A o;
o.*FiledPtr() = 10;
assert(o.X() == 10);
}
This question already has answers here:
Is it optional to use struct keyword before declaring a structure object?
(3 answers)
Closed 10 months ago.
I noticed that you can declare a pointer to a struct in two ways:
struct spell * spells;
and
spell * spells;
what is the difference?
In C, struct keyword must be used for declaring structure variables, but it is optional in C++.
For example, consider the following examples:
struct Foo
{
int data;
Foo* temp; // Error in C, struct must be there. Works in C++
};
int main()
{
Foo a; // Error in C, struct must be there. Works in C++
return 0;
}
Example 2
struct Foo
{
int data;
struct Foo* temp; // Works in both C and C++
};
int main()
{
struct Foo a; // Works in both C and C++
return 0;
}
In the above examples, temp is a data member that is a pointer to non-const Foo.
This question already has answers here:
Is there any way in C++ to refer to a function template while neither calling it nor supplying its template parameters?
(4 answers)
Convert template function to generic lambda
(2 answers)
How do I make a functor out of an arbitrary function?
(2 answers)
Closed 2 years ago.
I have a function returning a std::pair, which I want to use to call an overloaded member function. I am not aware of any way to unpack a tuple in-place (which I'd prefer), so std::apply is my best bet, I think.
This works with a combination of tuple_cat and forward_as_tuple when the member function is not overloaded, but otherwise, the compiler complains about an unresolved overload.
That is understandable, given that std::apply just takes a function pointer, but I'd still like to know whether it can be made to work in a readable way.
Here's some example code:
#include <iostream>
#include <tuple>
#include <utility>
std::pair<int, int> func() { return { 0, 1 }; }
class MyClass {
public:
int memFun(int);
int memFun(int, int);
};
int MyClass::memFun(int i){ return 2*i; }
int MyClass::memFun(int a, int b){ return a+b; }
int main(){
MyClass myClass;
/* works */
std::pair pair { func() };
int number { myClass.memFun( pair.first, pair.second ) };
/* doesn't work (unresolved overload) */
int number { std::apply(
&MyClass::memFun,
std::tuple_cat( std::forward_as_tuple(myClass), func() )
) };
std::cout << number << "\n";
return 0;
}
Is there an elegant (=readable) way to do this without a temporary?
I only found this question and so far don't think it fully applies.
This question already has answers here:
How to call through a member function pointer?
(2 answers)
Closed 4 years ago.
I try to give a reference to a member function (func) to a function (Multiprotocol) and call it with an object (z) of this class.
But i have problems with the line
result = z.*f(std::forward<Args>(args)...));
I tried things like
z.(*f) (std::forward<Args>(args)...))
and
z.(*(&f)(std::forward<Args>(args)...));
But i don't knwow how to this. Can anybody help me?
class test
{
public:
static int func()
{
std::cout << "in func";
}
};
class MultiProtocol
{
public:
template<typename Function, typename... Args>
bool functionImpl(Function f, Args&&... args)
{
int result = 0;
result = z.*f(std::forward<Args>(args)...));
return result;
}
private:
test z;
};
Main is:
int main()
{
MultiProtocol rr;
rr.functionImpl(&test::func);
}
Nearly, it is:
(z.*f)(std::forward<Args>(args)...)
Cannot be
z.(*f) (std::forward<Args>(args)...))
z.(*(&f)(std::forward<Args>(args)...));
as we should use operator .* (or ->*).
z.*f(std::forward<Args>(args)...) would be valid if f(args...) would return int test::* (pointer on member) as it is actually z .* (f(std::forward<Args>(args)...)).
This question already has an answer here:
decltype in class method declaration: error when used before "referenced" member is declared
(1 answer)
Closed 8 years ago.
My compiler is GCC 4.9.0.
struct A {
int n;
auto f() -> decltype(n) { // OK
return n;
}
};
struct B {
auto f() -> decltype(n) { // error: 'n' was not declared in this scope
return n;
}
int n;
};
int main() {
return A().f() + B().f();
}
Why does the order of the class members matter?
The declarations are compiled in order. It's the same reason you can't write:
int y = x;
int x = 5;
The bodies of inline functions (incl. c-tor initiailizer lists) are processed later (parsed first of course, but the names aren't looked up until after the class definition is complete) so they can refer to class members that are on later lines.