friend

c++ friend operator template specialization

亡梦爱人 提交于 2021-02-07 09:37:34
问题 I have a generalized modulo struct called quotient_ring . The relevant bits are shown below. template <typename R = long long> struct quotient_ring{ using Q = quotient_ring; R x, m; ... template <typename T> friend constexpr std::basic_ostream<T> &operator<< (std::basic_ostream<T> &str, const Q &q){ return str << '(' << q.x << ")%(" << q.m << ')'; } }; This operator << would print something like 2 mod 7 as (2)%(7) . The reason I need the brackets is because the type R can become very nested.

Friend, private function, template alias, and decltype… is clang correct in rejecting this?

こ雲淡風輕ζ 提交于 2021-02-07 06:55:25
问题 In the following code (godbolt link): #include <utility> struct Friend { class Inner { friend struct Friend; int function() { return 0; } }; using DirectResult = decltype(std::declval<Inner>().function()); template <typename T> using IndirectResult = decltype(std::declval<T>().function()); }; int main() { Friend::DirectResult direct{}; Friend::IndirectResult<Friend::Inner> indirect{}; return direct + indirect; } Clang is perfectly happy with the use of DirectResult , but will complaing that

template object's template friend functions and namespaces

一世执手 提交于 2021-02-07 06:13:05
问题 In the following C++ example code, GCC 6 and Clang 3.8 disagree on what the correct behaviour is: This contrived example "works" -- as in the test() function returns o.p in GCC. In clang, it calls the (undefined) function get<int, int, float, double> : template<typename ...Args> class obj { bool p = false; template<typename T, typename... Args2> friend T get(const obj<Args2...> &o) { return o.p; } }; template<typename T, typename... Args> T get(const obj<Args...> &o); bool test(const obj<int,

template object's template friend functions and namespaces

家住魔仙堡 提交于 2021-02-07 06:09:57
问题 In the following C++ example code, GCC 6 and Clang 3.8 disagree on what the correct behaviour is: This contrived example "works" -- as in the test() function returns o.p in GCC. In clang, it calls the (undefined) function get<int, int, float, double> : template<typename ...Args> class obj { bool p = false; template<typename T, typename... Args2> friend T get(const obj<Args2...> &o) { return o.p; } }; template<typename T, typename... Args> T get(const obj<Args...> &o); bool test(const obj<int,

Friend Function of Class produces error: “no '___' member function declared”

拥有回忆 提交于 2021-02-05 08:36:08
问题 I have a class, and I am trying to create a friend function to operate on the data of that class. Here is an example of what I am trying to do: // test.hpp class test { public: friend void friendly_function(); private: int data; }; void test::friendly_function() { data = 0; } However, the compiler spits out an error: test.hpp:23:34: error: no ‘void test::friendly_function()’ member function declared in class ‘test’ I know I can declare operators in this way, like so: class test { public:

Friend Function of Class produces error: “no '___' member function declared”

醉酒当歌 提交于 2021-02-05 08:35:39
问题 I have a class, and I am trying to create a friend function to operate on the data of that class. Here is an example of what I am trying to do: // test.hpp class test { public: friend void friendly_function(); private: int data; }; void test::friendly_function() { data = 0; } However, the compiler spits out an error: test.hpp:23:34: error: no ‘void test::friendly_function()’ member function declared in class ‘test’ I know I can declare operators in this way, like so: class test { public:

Friend Function, expected Primary Expression before . token

偶尔善良 提交于 2021-01-29 20:33:55
问题 So there are two classes in separate header files Customer. h using namespace std; #include <iostream> class Customer{ friend void Display(); private: int number, zipCode; public: Customer(int N, int Z){ number = N; zipCode = Z; } }; City. h using namespace std; #include #include "Customer.h" class City{ friend void Display(); private: int zipCode; string city, state; public: City(int Z, string C, string S){ zipCode = Z; city = C; state = S; } }; my main.cpp is as follows #include "City.h"

Template friend function of template class

♀尐吖头ヾ 提交于 2021-01-28 05:51:29
问题 I am wondering how to make a function friend of a class and define the function outside class if the function's template arguments include but are not limited to the class's template arguments. For example, I have the following template class and template friend function: template<int N> class Matrix; template<typename T, int N> Matrix<N> operator*(const Matrix<N> &m1, const T &m2); // class definition template<int N> class Matrix{ template<typename T> friend Matrix<N> operator* (const Matrix

vect.hpp:13:33: error: declaration of ‘operator<<’ as non-function

偶尔善良 提交于 2021-01-28 05:31:07
问题 I have this error vect.hpp:13:33: error: declaration of ‘operator<<’ as non-function for the code : #include <iostream> template<unsigned d> class Vect{ protected: double * coord; public: Vect() {for(int i=0, i<d, i++){*(coord+i)=0;}} ~Vect(){delete coord; coord=nullptr;} Vect(const Vect &); double operator=(const Vect &); double operator[](unsigned i) const{return *(coord+i);} friend std::ostream & operator<< <>(std::ostream &, const Vect<d> &); }; for the line : friend std::ostream &

Template Specialization for Private Types

拈花ヽ惹草 提交于 2021-01-27 12:31:23
问题 I have a generic algorithm that needs to access its template type's traits. There is a trait class that can be specialized for providing these traits. When using this algorithm within my class, I'd like to use it with a private type defined within the class. However, specialization can only happen within namespace or global scope where my class is inaccessible. class A { struct Secret {}; }; template <typename T> struct Trait {}; // Inaccessible type ----vvvvvvvvv template <> struct Trait<A: